Re: int.to_bytes() for a single byte

2018-11-07 Thread Thomas Jollans
On 07/11/2018 05:22, jlada...@itu.edu wrote:
> On Tuesday, November 6, 2018 at 7:19:09 PM UTC-8, Terry Reedy wrote:
>> On 11/6/2018 9:30 PM, j...y@it.u wrote:
>>
>>> b = i.to_bytes(1, "big")
>>>
>>> Is there another function which provides a more logical interface to this 
>>> straightforward task?
>>
>> Yes
>>  >>> 33 .to_bytes(1, 'big')
>> b'!'
>>  >>> bytes((33,))
>> b'!'
> 
> Thanks Terry, that's what I was looking for.
> 
> I had tried using the bytes() constructor directly, and was getting a byte 
> array of zeros, of the length specified by the integer.  That's in the 
> documentation, and it might be useful, but I haven't seen an obvious use 
> case, and it isn't what I wanted.  Wrapping the integer in a tuple solves the 
> problem of it being interpreted as a length.
> 

A bytes is a sequence of 8-bit integers, which is why the constructor
takes a sequence (or, well, iterable) of integers.

>>> bytes([49,50,51])
b'123'
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python

2018-11-07 Thread srinivasan
After changing the line to *"cmd = "blkid -o export %s | grep \'TYPE\' |
cut -d\"=\" -f3" % fs"*, Now I dont see the error "SyntaxError: can't
assign to literal"
This is not returning exactly "*vfat*" instead of this, it is returning as "*
/dev/mmcblk1p1: LABEL="efi" UUID="1084-AA42" TYPE="vfat"* "

Could you please help me  as it seems to be like grep and cut commands are
not working in the above line ie., on *cmd = "blkid -o export %s | grep
\'TYPE\' | cut -d\"=\" -f3" % fs*?

On Wed, Nov 7, 2018 at 9:41 AM Avi Gross  wrote:

> I may be missing something but it looks like the embedded double quotes
> may be a problem in this:
>
> cmd = "blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % ...
>
> if you use single quotes as in:
>
> cut -d"="
>
> becomes
>
> cut -d'='
>
> or escape the double quotes with \" and so on ...
>
> The above seems to be seen as:
>
> cmd=ALPHA=BETA % ...
>
> where ALPHA="blkid -o export %s | grep 'TYPE' | cut -d"
> and BETA=" -f3"
>
> Other issues may also be there.
> -Original Message-
> From: Tutor  On Behalf Of
> Alan Gauld via Tutor
> Sent: Tuesday, November 6, 2018 7:37 PM
> To: tu...@python.org
> Cc: python-...@python.org
> Subject: Re: [Tutor] SyntaxError: can't assign to literal while using
> ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using
> subprocess module in Python
>
> On 06/11/2018 18:07, srinivasan wrote:
>
> > bash command in python using subprocess module, I ma seeing the below
> > *cmd = "blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" %
> > (fs)*
>
> In general you should try to do as little as possible using bash and
> subprocess. Especially try to avoid long pipelines since you are starting a
> new OS process for every element in the pipeline. That means, in your case,
> you are running 4 processes to get your result - Python, blkid, grep and cut
>
> Python is designed to do much of what the shell command can do almost as
> easily and much more efficiently (no new processes being started).
>
> In this case just execute the blkid bit in bash because its too difficult
> to replicate simply in Python. Then use Python to search for the TYPE lines
> and slice them to size.
>
> That will, in turn, simplify your command string and remove the issue of
> multiple quotes.
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> ___
> Tutor maillist  -  tu...@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
> ___
> Tutor maillist  -  tu...@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Does this behavior have a better design pattern?

2018-11-07 Thread lampahome
I have two categories A,B, and A has 2 items A1,A2
B have 2 items B1, B2.

I have two class A and B, and A will handle A1,A2, B handle B1,B2.

I want to parse one of A1,A2,B1,B2 to script and generate the corresponding
class(object).

Ex: Both in class A and B, all have func1(), func2().
What I thought to design is below:
-
class Data(object):
def __Init__():
...
 def func1(self):
pass

def func2(self):
 pass

class A(Data):
def __Init__():
...
 def func1(self):
A_do()

def func2(self):
 A_does()

class B(Data):
def __Init__():
...
 def func1(self):
B_do()

def func2(self):
 B_does()

def get_class(obj):
if obj == 'A1' or obj == 'A2':
return A(obj)
 else:
return B(obj)

# A = get_class(A1)
# B = get_class(B2)

-

Above is I thought to make code clear, and this pattern is called simple
factory?

*Is there better design pattern for me?*

thanks
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Overwhelmed by the Simplicity of Python. Any Recommendation?

2018-11-07 Thread Thomas Jollans
On 06/11/2018 22:51, Lie Ryan wrote:
>> I like to step through my code line by line,
>> it's impossible to do it with
>> object-oriented programming language.
> 
> I suggest pudb, it's a curses based debugger, which is nicer than pdb, but 
> doesn't require tedious IDE setup.

I'll just take this opportunity to point out (for those that don't know)
that Visual Studio Code (an open source cross-platform programmer's text
editor of the same calibre as Sublime or Atom, not an IDE) has great
support for (graphical) debugging of Python code. Not tedious to set up,
particularly. Obviously there are plugins for other editors, but they're
usually not this well-integrated.

> 
>> Also, there's no good REPL IDE. 
> 
> Not quite sure what you meant by REPL IDE, but did you try IPython
> 

If you find yourself wanting a debugger in an IPython/Jupyter notebook,
ipdb is pretty nice.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python

2018-11-07 Thread Brian J. Oney via Python-list
On Wed, 2018-11-07 at 10:22 +0100, srinivasan wrote:
> blkid -o export %s | grep \'TYPE\' | cut -d\"=\" -f3

You don't need to escape the single quotes.
Try either:

"blkid -o export %s | grep 'TYPE' | cut -d'=' -f3"
or:
'blkid -o export %s | grep "TYPE" | cut -d"=" -f3'
or:
"blkid -o export %s | grep \"TYPE\" | cut -d\"=\" -f3"

HTH
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Does this behavior have a better design pattern?

2018-11-07 Thread Peter Otten
lampahome wrote:

> Above is I thought to make code clear, and this pattern is called simple
> factory?

This is a factory function:

> def get_class(obj):
> if obj == 'A1' or obj == 'A2':
> return A(obj)
>  else:
> return B(obj)

The rest is just cruft ;)
> 
> *Is there better design pattern for me?*

If A does B to C, is that a crime?

Your problem description suffers from overgeneralisation.

Generally speaking you get better solutions when you ask yourself
"How can I solve this problem efficiently?"
rather than
"What fancy design patterns can I use while solving this problem?"

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python

2018-11-07 Thread srinivasan
Even after changing as per the below
"blkid -o export %s | grep 'TYPE' | cut -d'=' -f3"
or:
'blkid -o export %s | grep "TYPE" | cut -d"=" -f3'
or:
"blkid -o export %s | grep \"TYPE\" | cut -d\"=\" -f3"

Still my output is:
*/dev/mmcblk1p1: LABEL="efi" UUID="1084-AA42" TYPE="vfat"*

My expected output should be only:
*vfat*

Could you guys please do the needful?


On Wed, Nov 7, 2018 at 11:10 AM Brian J. Oney 
wrote:

> On Wed, 2018-11-07 at 10:22 +0100, srinivasan wrote:
> > blkid -o export %s | grep \'TYPE\' | cut -d\"=\" -f3
>
> You don't need to escape the single quotes.
> Try either:
>
> "blkid -o export %s | grep 'TYPE' | cut -d'=' -f3"
> or:
> 'blkid -o export %s | grep "TYPE" | cut -d"=" -f3'
> or:
> "blkid -o export %s | grep \"TYPE\" | cut -d\"=\" -f3"
>
> HTH
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python

2018-11-07 Thread Qian Cai
srinivasan  wrote:
> Even after changing as per the below
> "blkid -o export %s | grep 'TYPE' | cut -d'=' -f3"
> or:
> 'blkid -o export %s | grep "TYPE" | cut -d"=" -f3'
> or:
> "blkid -o export %s | grep \"TYPE\" | cut -d\"=\" -f3"
> 
> Still my output is:
> */dev/mmcblk1p1: LABEL="efi" UUID="1084-AA42" TYPE="vfat"*
> 
> My expected output should be only:
> *vfat*
> 
> Could you guys please do the needful?
> 
> 
Perfect place to use sed instead of grep/cut.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python

2018-11-07 Thread Chris Angelico
On Wed, Nov 7, 2018 at 11:36 PM Qian Cai  wrote:
>
> srinivasan  wrote:
> > Even after changing as per the below
> > "blkid -o export %s | grep 'TYPE' | cut -d'=' -f3"
> > or:
> > 'blkid -o export %s | grep "TYPE" | cut -d"=" -f3'
> > or:
> > "blkid -o export %s | grep \"TYPE\" | cut -d\"=\" -f3"
> >
> > Still my output is:
> > */dev/mmcblk1p1: LABEL="efi" UUID="1084-AA42" TYPE="vfat"*
> >
> > My expected output should be only:
> > *vfat*
> >
> > Could you guys please do the needful?
> >
> >
> Perfect place to use sed instead of grep/cut.

... or to use subprocess.check_output() to run just the blkid command,
and then do the parsing in Python.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python

2018-11-07 Thread srinivasan
Some I managed to fix temporarily as below, might be useful for others.
Also please correct me if anything wrong or for any improvements in the
below

cmd = "blkid -o export %s" % partition_path
out = self._helper.execute_cmd_output_string(cmd)
var = out.split("TYPE=", 1)[1]
quoted = re.compile('(?<=^\")[^"]*')
for string in quoted.findall(var):
return string

On Wed, Nov 7, 2018 at 1:39 PM Chris Angelico  wrote:

> On Wed, Nov 7, 2018 at 11:36 PM Qian Cai  wrote:
> >
> > srinivasan  wrote:
> > > Even after changing as per the below
> > > "blkid -o export %s | grep 'TYPE' | cut -d'=' -f3"
> > > or:
> > > 'blkid -o export %s | grep "TYPE" | cut -d"=" -f3'
> > > or:
> > > "blkid -o export %s | grep \"TYPE\" | cut -d\"=\" -f3"
> > >
> > > Still my output is:
> > > */dev/mmcblk1p1: LABEL="efi" UUID="1084-AA42" TYPE="vfat"*
> > >
> > > My expected output should be only:
> > > *vfat*
> > >
> > > Could you guys please do the needful?
> > >
> > >
> > Perfect place to use sed instead of grep/cut.
>
> ... or to use subprocess.check_output() to run just the blkid command,
> and then do the parsing in Python.
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Unable to remove python from my computer.

2018-11-07 Thread Rhodri James
I assume "on a PC" actually means "on Windows".  Redirected back to the 
list since I neither know nor care about wrangling Python on Windows.


On 07/11/2018 12:27, Jack Gilbert wrote:

on a PC:

click start, (click on the window, lower left hand corner)[ in 8.1]
Control Panel
Programs and Features

you will see a list of programs installed on your machine in alphabetical
order
look for Python x.x.x(32 bit)  click to highlight
click uninstall  near the top of the screen
wait
wait
wait
the machine might ask you,, are you sure you want to uninstall this
program?  click ok or yes
wait
wait
wait
wait some more

you might see the status bar move to the right as the machine clears the
program

wait
wait

the machine will say,, this progam has been uninstalled

you should see or rather not see the program in the list and any python
icon that you have on your desktop will also be removed.

I have  Python 3.6.4(32-bit) on a 64bit PC with 5TB HDD, Dell machine,
running 8.1




Virus-free.
www.avg.com

<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

On Tue, Nov 6, 2018 at 5:42 AM Rhodri James  wrote:


On 06/11/2018 09:25, Thomas Jollans wrote:

On 2018-11-06 10:05, Varshit Jain wrote:

Hi Python Support Team,


I just want to remove python 3.6.6 from my computer. I am unable to do
it. Please find attached video that describe my problem.


Use your words, friend!

(this list is text-only)


More exactly, I'm afraid the mailing list has stripped off the video you
attached, so we can't watch it.  I'm afraid I wouldn't in any case; I
learned long ago not to open attachments from people I don't already know.

Could you please describe to us your problem?  Are you using Windows,
Linux, Mac or something else?  Copy any error messages the uninstaller
might have given you, preferably cutting and pasting rather than just
retyping them (it's easy to mistype something critical!).  Please don't
send us screen shots, those will just get stripped off by the mailing
list as well.

--
Rhodri James *-* Kynesim Ltd
--
https://mail.python.org/mailman/listinfo/python-list






--
Rhodri James *-* Kynesim Ltd
--
https://mail.python.org/mailman/listinfo/python-list


Re: [Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python

2018-11-07 Thread Chris Angelico
On Wed, Nov 7, 2018 at 11:42 PM srinivasan  wrote:
>
> Some I managed to fix temporarily as below, might be useful for others. Also 
> please correct me if anything wrong or for any improvements in the below
>
> cmd = "blkid -o export %s" % partition_path
> out = self._helper.execute_cmd_output_string(cmd)
> var = out.split("TYPE=", 1)[1]
> quoted = re.compile('(?<=^\")[^"]*')
> for string in quoted.findall(var):
> return string

Leaving aside the fact that MS Comic Sans is known to the State of
California to cause cancer, this code is probably okay if you don't
mind it being overengineered. Here's a much simpler version, albeit
untested:

out = subprocess.check_output(["blkid", "-o", "export", partition_path])
for line in out.split("\n"):
item, value = line.split("=")
if item == "TYPE": return value

No helper needed. Safe against command injection. Uses the known
format of the command's output; if you want other information as well
as the type, you could get that too.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python

2018-11-07 Thread Ben Bacarisse
srinivasan  writes:

> Even after changing as per the below
> "blkid -o export %s | grep 'TYPE' | cut -d'=' -f3"
> or:
> 'blkid -o export %s | grep "TYPE" | cut -d"=" -f3'
> or:
> "blkid -o export %s | grep \"TYPE\" | cut -d\"=\" -f3"
>
> Still my output is:
> */dev/mmcblk1p1: LABEL="efi" UUID="1084-AA42" TYPE="vfat"*
>
> My expected output should be only:
> *vfat*
>
> Could you guys please do the needful?

Why not

  blkid %s -o value --match-tag TYPE

?  Or, better still,

  lsblk %s -n -o FSTYPE

It's not easy to answer your question about fixing the line you have,
because the output you are getting it not consistent with what you are
showing.  (And I can't find the original post that presumably has Python
code I could run myself.)

The format I get with -o export is:

DEVNAME=/dev/sda1
UUID=2726bf5f-2655-4986-815d-e4532374f218
TYPE=ext3
PARTUUID=000453d3-01

for which

  "blkid %s -o export | grep TYPE | cut -c6-"

would work.  Alternatively I get the result you want from

  "blkid %s -o export | grep TYPE | cut -d= -f2"

Note that "TYPE" and "=" don't really need to be quoted at all.

Someone suggested sed, and that too can be used like this:

  blkid %s -o export | sed -ne '/TYPE=/s///p'

-- 
Ben.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python

2018-11-07 Thread srinivasan
Many Thanks a lot , I can use for reliably "lsblk %s -n -o FSTYPE"  in the
reused code of mine as below

cmd = "lsblk %s -n -o FSTYPE" % partition_path
return self._helper.execute_cmd_output_string(cmd)

I really appreciate for all your support w.r.t this..

I feel I have kick started my learning in python :)

Have a great day ahead!


On Wed, Nov 7, 2018 at 3:11 PM Ben Bacarisse  wrote:

> srinivasan  writes:
>
> > Even after changing as per the below
> > "blkid -o export %s | grep 'TYPE' | cut -d'=' -f3"
> > or:
> > 'blkid -o export %s | grep "TYPE" | cut -d"=" -f3'
> > or:
> > "blkid -o export %s | grep \"TYPE\" | cut -d\"=\" -f3"
> >
> > Still my output is:
> > */dev/mmcblk1p1: LABEL="efi" UUID="1084-AA42" TYPE="vfat"*
> >
> > My expected output should be only:
> > *vfat*
> >
> > Could you guys please do the needful?
>
> Why not
>
>   blkid %s -o value --match-tag TYPE
>
> ?  Or, better still,
>
>   lsblk %s -n -o FSTYPE
>
> It's not easy to answer your question about fixing the line you have,
> because the output you are getting it not consistent with what you are
> showing.  (And I can't find the original post that presumably has Python
> code I could run myself.)
>
> The format I get with -o export is:
>
> DEVNAME=/dev/sda1
> UUID=2726bf5f-2655-4986-815d-e4532374f218
> TYPE=ext3
> PARTUUID=000453d3-01
>
> for which
>
>   "blkid %s -o export | grep TYPE | cut -c6-"
>
> would work.  Alternatively I get the result you want from
>
>   "blkid %s -o export | grep TYPE | cut -d= -f2"
>
> Note that "TYPE" and "=" don't really need to be quoted at all.
>
> Someone suggested sed, and that too can be used like this:
>
>   blkid %s -o export | sed -ne '/TYPE=/s///p'
>
> --
> Ben.
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Overwhelmed by the Simplicity of Python. Any Recommendation?

2018-11-07 Thread MRAB

On 2018-11-07 09:20, Thomas Jollans wrote:

On 06/11/2018 22:51, Lie Ryan wrote:

I like to step through my code line by line,
it's impossible to do it with
object-oriented programming language.


I suggest pudb, it's a curses based debugger, which is nicer than pdb, but 
doesn't require tedious IDE setup.


I'll just take this opportunity to point out (for those that don't know)
that Visual Studio Code (an open source cross-platform programmer's text
editor of the same calibre as Sublime or Atom, not an IDE) has great
support for (graphical) debugging of Python code. Not tedious to set up,
particularly. Obviously there are plugins for other editors, but they're
usually not this well-integrated.


I find that the code does run more slowly, though.



Also, there's no good REPL IDE. 


Not quite sure what you meant by REPL IDE, but did you try IPython



If you find yourself wanting a debugger in an IPython/Jupyter notebook,
ipdb is pretty nice.


--
https://mail.python.org/mailman/listinfo/python-list


Re: Overwhelmed by the Simplicity of Python. Any Recommendation?

2018-11-07 Thread Thomas Jollans
On 07/11/2018 21:31, MRAB wrote:
> On 2018-11-07 09:20, Thomas Jollans wrote:
>> On 06/11/2018 22:51, Lie Ryan wrote:
 I like to step through my code line by line,
 it's impossible to do it with
 object-oriented programming language.
>>>
>>> I suggest pudb, it's a curses based debugger, which is nicer than
>>> pdb, but doesn't require tedious IDE setup.
>>
>> I'll just take this opportunity to point out (for those that don't know)
>> that Visual Studio Code (an open source cross-platform programmer's text
>> editor of the same calibre as Sublime or Atom, not an IDE) has great
>> support for (graphical) debugging of Python code. Not tedious to set up,
>> particularly. Obviously there are plugins for other editors, but they're
>> usually not this well-integrated.
>>
> I find that the code does run more slowly, though.

Can't argue with that.

I'm not totally sold on vscode, either. But I enjoy the debugger!


> 
>>>
 Also, there's no good REPL IDE. 
>>>
>>> Not quite sure what you meant by REPL IDE, but did you try IPython
>>>
>>
>> If you find yourself wanting a debugger in an IPython/Jupyter notebook,
>> ipdb is pretty nice.
>>

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Does this behavior have a better design pattern?

2018-11-07 Thread lampahome
>
>
> The rest is just cruft ;)
> >
> > *Is there better design pattern for me?*
>
> If A does B to C, is that a crime?
>
No


> Your problem description suffers from overgeneralisation.
>
> Generally speaking you get better solutions when you ask yourself
> "How can I solve this problem efficiently?"
>
>
In another hand, how can I solve this problem efficiently?
or any pythonic way to solve this?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Overwhelmed by the Simplicity of Python. Any Recommendation?

2018-11-07 Thread Michael Torrie
On 11/07/2018 01:31 PM, MRAB wrote:
> On 2018-11-07 09:20, Thomas Jollans wrote:
>> I'll just take this opportunity to point out (for those that don't know)
>> that Visual Studio Code (an open source cross-platform programmer's text
>> editor of the same calibre as Sublime or Atom, not an IDE) has great
>> support for (graphical) debugging of Python code. Not tedious to set up,
>> particularly. Obviously there are plugins for other editors, but they're
>> usually not this well-integrated.
>>
> I find that the code does run more slowly, though.

Why?  Python is python, isn't it?  Usually code runs slower in a
debugger.  Isn't VS code using pdb under the hood?
-- 
https://mail.python.org/mailman/listinfo/python-list