Re: How To Change Package Representation on Shell?

2020-04-01 Thread Abdur-Rahmaan Janhangeer
Having fun with packages

Since i don't master packaging completely thought there was a __repr__.py
protocol nearby!
Might be useful maybe to replace it by a help message

Kind Regards,

Abdur-Rahmaan Janhangeer
compileralchemy.com  | github

Mauritius


On Thu, Apr 2, 2020 at 12:41 AM Chris Angelico  wrote:

> On Thu, Apr 2, 2020 at 7:37 AM Rhodri James  wrote:
> >
> > On 01/04/2020 21:22, Abdur-Rahmaan Janhangeer wrote:
> > > Greetings list,
> > >
> > > I have a custom package.
> > >
> >  import package
> >  package
> > > '>
> > >
> > > what do i have to modify from my package to have like
> > >
> >  package
> > > Hi!
> >
> > Do you mean "How do I override the str() or repr() of a module"?  I
> > don't think you can.
> >
>
> Not easily. It is possible but only by replacing the module as you're
> importing it.
>
> Also, I'm not sure why you would even need/want to do this - Abdur,
> can you elaborate on the use-case here?
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How To Change Package Representation on Shell?

2020-04-01 Thread Chris Angelico
On Thu, Apr 2, 2020 at 7:37 AM Rhodri James  wrote:
>
> On 01/04/2020 21:22, Abdur-Rahmaan Janhangeer wrote:
> > Greetings list,
> >
> > I have a custom package.
> >
>  import package
>  package
> > '>
> >
> > what do i have to modify from my package to have like
> >
>  package
> > Hi!
>
> Do you mean "How do I override the str() or repr() of a module"?  I
> don't think you can.
>

Not easily. It is possible but only by replacing the module as you're
importing it.

Also, I'm not sure why you would even need/want to do this - Abdur,
can you elaborate on the use-case here?

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


Re: How To Change Package Representation on Shell?

2020-04-01 Thread Rhodri James

On 01/04/2020 21:22, Abdur-Rahmaan Janhangeer wrote:

Greetings list,

I have a custom package.


import package
package

'>

what do i have to modify from my package to have like


package

Hi!


Do you mean "How do I override the str() or repr() of a module"?  I 
don't think you can.


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


Re: How to change '\\' to '\'

2018-10-02 Thread Michael Torrie
On 10/02/2018 12:48 AM, Ethan Furman wrote:
> Even for two-person, private email discussions I prefer the interleaved 
> replies -- in a week when I have to remind myself what was discussed it 
> is much easier to comprehend.

Absolutely. I've been saved from embarrassment countless times because
while editing the quote so I could respond to it point by point, I
realized I had misread the the original poster.

Top posting usually indicates that the person replying to my email never
really read it, and is just replying to what he thought I said or asked.
 Frustrating to no end.  This problem is endemic in corporate
communication.  Corporate email is completely nonfunctional as a means
of communication, largely because of the top posting culture.  Well
communication is a problem in general in corporations because of the
personalities that tend to gravitate towards the management end of
things. Top posting just makes the communication that much worse!

> Yes.  It is extremely annoying when someone top posts and leaves the 
> entire rest of the discussion still attached at the bottom.

Or when they bottom post without trimming, or even when they interleave
their responses without trimming.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to change '\\' to '\'

2018-10-02 Thread Chris Angelico
On Wed, Oct 3, 2018 at 12:05 AM Dennis Lee Bieber  wrote:
>
> On Tue, 2 Oct 2018 10:17:27 +0800, Jach Fong 
> declaimed the following:
>
>
> >It was supposed that most discussant want to see the reply message
> >instantly when they open the mail. They already know what is going on
> >and no need to pass through all those previous message. "top posting"
> >seems more reasonable to me:-)
> >
>
> Such behavior reflects
> 1) personal (1 to 1) replies, where the recipient should already know the
> subject of the matter (having written it in the first place);

Which assumes that the recipient of your message has sent few enough
messages that s/he remembers the content of all of them. I don't know
about you, but that certainly isn't true of me. If you're replying to
anything more than a week old, I'm going to be looking at the quoted
context to see what I said.

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


Re: How to change '\\' to '\'

2018-10-02 Thread Ethan Furman

On 10/01/2018 11:10 PM, Christian Gollwitzer wrote:

Am 02.10.18 um 04:17 schrieb Jach Fong:



It was supposed that most discussant want to see the reply message
instantly when they open the mail. They already know what is going on
and no need to pass through all those previous message. "top posting"
seems more reasonable to me:-)


You assume that everybody who reads it has already read all the previous 
discussion. That may be true in a private discussion, but on Usenet 
often people scan over the postings and don't recall all the details.


Even for two-person, private email discussions I prefer the interleaved 
replies -- in a week when I have to remind myself what was discussed it 
is much easier to comprehend.


Also, "intermixed posting" makes it easier to reply to several points 
individually. You should shorten the quote to the releveant bit that 
your reply belongs to.


Yes.  It is extremely annoying when someone top posts and leaves the 
entire rest of the discussion still attached at the bottom.


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


Re: How to change '\\' to '\'

2018-10-02 Thread Christian Gollwitzer

Am 02.10.18 um 04:17 schrieb Jach Fong:

It was supposed that most discussant want to see the reply message
instantly when they open the mail. They already know what is going on
and no need to pass through all those previous message. "top posting"
seems more reasonable to me:-)


You assume that everybody who reads it has already read all the previous 
discussion. That may be true in a private discussion, but on Usenet 
often people scan over the postings and don't recall all the details.


Also, "intermixed posting" makes it easier to reply to several points 
individually. You should shorten the quote to the releveant bit that 
your reply belongs to.


Classic quote:

A: Because it reverses the logical flow of conversation.

Q: Why is top posting frowned upon?

A: Top-posting.

Q: What is the most annoying thing on usenet and in email?



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


Re: How to change '\\' to '\'

2018-10-01 Thread William Ray Wing via Python-list


> On Oct 1, 2018, at 10:17 PM, Jach Fong  wrote:
> 
> Thanks for your info about how Windows supports the forward slash.
> 
> I don't quit sure what is the meaning of "top posting" in your mail.
> If its meaning (forgive me if I was wrong) is where the reply was put
> in mail, I have reason of standing on the opposite side.
> 

Yes, exactly correct 

> It was supposed that most discussant want to see the reply message
> instantly when they open the mail. They already know what is going on
> and no need to pass through all those previous message. "top posting"
> seems more reasonable to me:-)
> 

The problem is that it is comparatively rare for there to be a singleton Q and 
immediate A.  Even seemingly simple questions frequently trigger fairly long 
discussions, which assume familiarity with the earlier discussion. AND this 
list is pretty much the place of record for people researching python IFAQs 
(infrequently asked questions). So, six months from now a Google search that 
turns up a relevant thread will make sense only if it can be read as in-lined 
comments like this. 

> --Jach
> 
> mm0fmf at 2018/10/2 AM 05:05 wrote:
>>> On 01/10/2018 10:19, Jach Fong wrote:
>>> Hmmm...strange, I didn't see Rick's mail:-(
>>> 
>>> Sure the forward slash is better, not to cause this confusion.
>>> But I am curious, since when, I mean, since which version
>>> Windows start to accept forward slash?
>>> 
>> First, stop top posting.
>> Second, ISTR that all Windows NT versions and versions derived from the NT 
>> codebase support forward slash in pathnames given to functions. It may go 
>> back further, but anything derived from NT works.  I can't remember which 
>> Windows command shells support it, probably PowerShell does.
> 
> ---
> This email has been checked for viruses by Avast antivirus software.
> https://www.avast.com/antivirus
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to change '\\' to '\'

2018-10-01 Thread Jach Fong

Thanks for your info about how Windows supports the forward slash.

I don't quit sure what is the meaning of "top posting" in your mail.
If its meaning (forgive me if I was wrong) is where the reply was put
in mail, I have reason of standing on the opposite side.

It was supposed that most discussant want to see the reply message
instantly when they open the mail. They already know what is going on
and no need to pass through all those previous message. "top posting"
seems more reasonable to me:-)

--Jach

mm0fmf at 2018/10/2 AM 05:05 wrote:

On 01/10/2018 10:19, Jach Fong wrote:

Hmmm...strange, I didn't see Rick's mail:-(

Sure the forward slash is better, not to cause this confusion.
But I am curious, since when, I mean, since which version
Windows start to accept forward slash?



First, stop top posting.

Second, ISTR that all Windows NT versions and versions derived from the 
NT codebase support forward slash in pathnames given to functions. It 
may go back further, but anything derived from NT works.  I can't 
remember which Windows command shells support it, probably PowerShell does.




---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

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


Re: How to change '\\' to '\'

2018-10-01 Thread mm0fmf

On 01/10/2018 10:19, Jach Fong wrote:

Hmmm...strange, I didn't see Rick's mail:-(

Sure the forward slash is better, not to cause this confusion.
But I am curious, since when, I mean, since which version
Windows start to accept forward slash?



First, stop top posting.

Second, ISTR that all Windows NT versions and versions derived from the 
NT codebase support forward slash in pathnames given to functions. It 
may go back further, but anything derived from NT works.  I can't 
remember which Windows command shells support it, probably PowerShell does.


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


Re: How to change '\\' to '\'

2018-10-01 Thread Peter J. Holzer
On 2018-10-01 17:19:25 +0800, Jach Fong wrote:
> Sure the forward slash is better, not to cause this confusion.
> But I am curious, since when, I mean, since which version
> Windows start to accept forward slash?

Since MS-DOS 2.0, i.e., before Windows even existed. Note that this is
only true for system call paraeters[1], not for command line parameters. 

So for your use case:

> > > > I want to use this "string" in a subprocess command as a parameter.

you probably need to use backslashes, not forward slashes (it depends on
the program you are calling).

hp

[1] And even there not always, as I've learned from the resident Windows
expert on this list.

-- 
   _  | Peter J. Holzer| we build much bigger, better disasters now
|_|_) || because we have much more sophisticated
| |   | h...@hjp.at | management tools.
__/   | http://www.hjp.at/ | -- Ross Anderson 


signature.asc
Description: PGP signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to change '\\' to '\'

2018-10-01 Thread Thomas Jollans
On 2018-10-01 11:19, Jach Fong wrote:
> Hmmm...strange, I didn't see Rick's mail:-(

That's because he's banned from the list, but still around on the newsgroup

> Sure the forward slash is better, not to cause this confusion.
> But I am curious, since when, I mean, since which version
> Windows start to accept forward slash?

Windows has always supported forward slashes in most contexts for
compatibility with UNIX software. (just not in cmd.exe IIRC)

> 
> --Jach
> 
> Alister ware via Python-list at 2018/10/1 PM 04:15 wrote:
>> On Sun, 30 Sep 2018 17:45:52 -0700, Rick Johnson wrote:
>>
>>> Jach Fong wrote:
 I get a string item, for example path[0], from path =
 os.get_exec_path()
 It's something like "\\Borland\\Bcc55\\Include", a Python string.
 I want to use this "string" in a subprocess command as a parameter.
 Obviously this command can only recognize "\Borland\Bcc55\Include".
 I know there must have an easy way to convert it, but just can't figure
 it out :-(
>>>
>>> I would suggest substituting all backslashes with forward slashes, that
>>> way, the printed version and the internal escaped version won't be
>>> confusing to you. And besides, which is easier to read?
>>>
>>> This?
>>>
>>>    "\\Borland\\Bcc55\\Include"
>>>
>>> Or this?
>>>
>>>    "/Borland/Bcc55/Include"
>>>
>>> ???
>>>
>>> 
>>
>> Practicality Beats Purity
>>
>>
>>
>>
> 
> ---
> This email has been checked for viruses by Avast antivirus software.
> https://www.avast.com/antivirus
> 


-- 
Thomas Jollans

m ☎ +31 (6) 42630259
e ✉ t...@tjol.eu
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to change '\\' to '\'

2018-10-01 Thread Jach Fong

Hmmm...strange, I didn't see Rick's mail:-(

Sure the forward slash is better, not to cause this confusion.
But I am curious, since when, I mean, since which version
Windows start to accept forward slash?

--Jach

Alister ware via Python-list at 2018/10/1 PM 04:15 wrote:

On Sun, 30 Sep 2018 17:45:52 -0700, Rick Johnson wrote:


Jach Fong wrote:

I get a string item, for example path[0], from path =
os.get_exec_path()
It's something like "\\Borland\\Bcc55\\Include", a Python string.
I want to use this "string" in a subprocess command as a parameter.
Obviously this command can only recognize "\Borland\Bcc55\Include".
I know there must have an easy way to convert it, but just can't figure
it out :-(


I would suggest substituting all backslashes with forward slashes, that
way, the printed version and the internal escaped version won't be
confusing to you. And besides, which is easier to read?

This?

   "\\Borland\\Bcc55\\Include"

Or this?

   "/Borland/Bcc55/Include"

???




Practicality Beats Purity






---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

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


Re: How to change '\\' to '\'

2018-10-01 Thread Alister ware via Python-list
On Sun, 30 Sep 2018 17:45:52 -0700, Rick Johnson wrote:

> Jach Fong wrote:
>> I get a string item, for example path[0], from path =
>> os.get_exec_path()
>> It's something like "\\Borland\\Bcc55\\Include", a Python string.
>> I want to use this "string" in a subprocess command as a parameter.
>> Obviously this command can only recognize "\Borland\Bcc55\Include".
>> I know there must have an easy way to convert it, but just can't figure
>> it out :-(
> 
> I would suggest substituting all backslashes with forward slashes, that
> way, the printed version and the internal escaped version won't be
> confusing to you. And besides, which is easier to read?
> 
> This?
> 
>   "\\Borland\\Bcc55\\Include"
> 
> Or this?
> 
>   "/Borland/Bcc55/Include"
> 
> ???
> 
> 

Practicality Beats Purity




-- 
沾衣欲湿杏花雨,吹面不寒杨柳风。
-- 志南《绝句》
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to change '\\' to '\'

2018-09-28 Thread Jach Fong

Yes, you are right, it's just the way Python display the '\'.
Thank you.

Gregory Ewing at 2018/9/28 PM 02:02 wrote:

Jach Fong wrote:

I get a string item, for example path[0], from path = os.get_exec_path()
It's something like "\\Borland\\Bcc55\\Include"


It doesn't actually have double backslashes in it, that's just a
result of how the string is being displayed. No conversion is
needed.



---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

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


Re: How to change '\\' to '\'

2018-09-28 Thread Gregory Ewing

Jach Fong wrote:

I get a string item, for example path[0], from path = os.get_exec_path()
It's something like "\\Borland\\Bcc55\\Include"


It doesn't actually have double backslashes in it, that's just a
result of how the string is being displayed. No conversion is
needed.

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


Re: How to change variable from list to float

2017-06-05 Thread Peter Pearson
On Mon, 5 Jun 2017 11:13:54 +0100, Paul Barry wrote:
> On 3 June 2017 at 15:42, Gary Barker  wrote:
>
>> I have searched for a solution to this but have not found a suitable
>> example.
>>
>> The attached code generates this error: Traceback (most recent call last):
>>   File "calcsignal.py", line 7, in 
>> siglevfromexist = 34.8 + existattn
>> TypeError: unsupported operand type(s) for +: 'float' and 'list'
>>
>> How do I convert the list variable (i.e. existattn) to a float?
>>
>> Operating details are:
>> Python 3.4.2
>> Debian 3.16.39-1+deb8u2 (2017-03-07) x86_64 GNU/Linux
>>
>> The following lines are the code in calcsignal.py:
>> azdegpattrev = -47.40715077970316
>> azattndic = {-0.9: [-0.55], -0.5: [-0.46], 3.5: [-21.0], 1.4: [-7.48],
>> 5.5: [-25.0], 0.0: [0.0], 1.9: [-21.0], 13.0: [-38.0], 15.0: [-39.0], 3.6:
>> [-25.0], 20.0: [-39.0], -1.4: [-7.48], 90.0: [-65.0], -0.4: [-0.39], 0.5:
>> [-0.46], 0.1: [-0.04], 1.0: [-1.31], -90.0: [-65.0], 40.0: [-42.0], 180.0:
>> [-65.0], 1.5: [-10.0], -1.2: [-3.69], 0.3: [-0.28], -0.3: [-0.28], 0.2:
>> [-0.15], -0.1: [-0.04], 1.1: [-2.34], -180.0: [-65.0], -0.2: [-0.15], 1.2:
>> [-3.69], -40.0: [-42.0], 0.4: [-0.39], -5.5: [-25.0], -1.5: [-10.0], -20.0:
>> [-39.0], 0.9: [-0.55], -3.5: [-21.0], -1.9: [-21.0], -15.0: [-39.0], -13.0:
>> [-38.0], 1.3: [-5.39], -1.3: [-5.39], -3.6: [-25.0], -1.0: [-1.31], -1.1:
>> [-2.34]}
>> azlist = [-0.9, -0.5, 3.5, 1.4, 5.5, 0.0, 1.9, 13.0, 15.0, 3.6, 20.0,
>> -1.4, 90.0, -0.4, 0.5, 0.1, 1.0, -90.0, 40.0, 180.0, 1.5, -1.2, 0.3, -0.3,
>> 0.2, -0.1, 1.1, -180.0, -0.2, 1.2, -40.0, 0.4, -5.5, -1.5, -20.0, 0.9,
>> -3.5, -1.9, -15.0, -13.0, 1.3, -1.3, -3.6, -1.0, -1.1]
>> azlist = [float(i) for i in azlist]
>> closestaz = min(azlist, key=lambda x: abs(x - azdegpattrev))
>> existattn = azattndic[closestaz]
>> siglevfromexist = 34.8 + existattn
>
> The value in existattn is a single element list.  The single element is a
> float, so just refer to it in your calculation, like so:
>
> siglevfromexist = 34.8 + existattn[0]

But as you do so, remember to wonder why the author of this code
made the elements of azattndic *lists* of values instead of simple
single values.  Then, worry that someday your corrected code, which
uses only the first value of the list, will be handed a list with
several values.  (True, it will execute without exception; but right
now, today, you know that if the list length is not 1, you're
confused.)

-- 
To email me, substitute nowhere->runbox, invalid->com.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to change variable from list to float

2017-06-05 Thread Paul Barry
The value in existattn is a single element list.  The single element is a
float, so just refer to it in your calculation, like so:

siglevfromexist = 34.8 + existattn[0]

Regards.

Paul.

On 3 June 2017 at 15:42, Gary Barker  wrote:

> I have searched for a solution to this but have not found a suitable
> example.
>
> The attached code generates this error: Traceback (most recent call last):
>   File "calcsignal.py", line 7, in 
> siglevfromexist = 34.8 + existattn
> TypeError: unsupported operand type(s) for +: 'float' and 'list'
>
> How do I convert the list variable (i.e. existattn) to a float?
>
> Operating details are:
> Python 3.4.2
> Debian 3.16.39-1+deb8u2 (2017-03-07) x86_64 GNU/Linux
>
> The following lines are the code in calcsignal.py:
> azdegpattrev = -47.40715077970316
> azattndic = {-0.9: [-0.55], -0.5: [-0.46], 3.5: [-21.0], 1.4: [-7.48],
> 5.5: [-25.0], 0.0: [0.0], 1.9: [-21.0], 13.0: [-38.0], 15.0: [-39.0], 3.6:
> [-25.0], 20.0: [-39.0], -1.4: [-7.48], 90.0: [-65.0], -0.4: [-0.39], 0.5:
> [-0.46], 0.1: [-0.04], 1.0: [-1.31], -90.0: [-65.0], 40.0: [-42.0], 180.0:
> [-65.0], 1.5: [-10.0], -1.2: [-3.69], 0.3: [-0.28], -0.3: [-0.28], 0.2:
> [-0.15], -0.1: [-0.04], 1.1: [-2.34], -180.0: [-65.0], -0.2: [-0.15], 1.2:
> [-3.69], -40.0: [-42.0], 0.4: [-0.39], -5.5: [-25.0], -1.5: [-10.0], -20.0:
> [-39.0], 0.9: [-0.55], -3.5: [-21.0], -1.9: [-21.0], -15.0: [-39.0], -13.0:
> [-38.0], 1.3: [-5.39], -1.3: [-5.39], -3.6: [-25.0], -1.0: [-1.31], -1.1:
> [-2.34]}
> azlist = [-0.9, -0.5, 3.5, 1.4, 5.5, 0.0, 1.9, 13.0, 15.0, 3.6, 20.0,
> -1.4, 90.0, -0.4, 0.5, 0.1, 1.0, -90.0, 40.0, 180.0, 1.5, -1.2, 0.3, -0.3,
> 0.2, -0.1, 1.1, -180.0, -0.2, 1.2, -40.0, 0.4, -5.5, -1.5, -20.0, 0.9,
> -3.5, -1.9, -15.0, -13.0, 1.3, -1.3, -3.6, -1.0, -1.1]
> azlist = [float(i) for i in azlist]
> closestaz = min(azlist, key=lambda x: abs(x - azdegpattrev))
> existattn = azattndic[closestaz]
> siglevfromexist = 34.8 + existattn
> --
> https://mail.python.org/mailman/listinfo/python-list
>



-- 
Paul Barry, t: @barrypj  - w:
http://paulbarry.itcarlow.ie - e: paul.ba...@itcarlow.ie
Lecturer, Computer Networking: Institute of Technology, Carlow, Ireland.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to change the number into the same expression's string and vice versa?

2015-01-19 Thread Peter Otten
contro opinion wrote:

 In the python3 console:
 
  a=18
  b='18'
  str(a) == b
 True
  int(b) == a
 True
 
 
 Now how to change a1,a2,a3  into b1,b2,b3 and vice versa?
 a1=0xf4
 a2=0o36
 a3=011
 
 b1='0xf4'
 b2='0o36'
 b3='011'

Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
[GCC 4.8.2] on linux
Type help, copyright, credits or license for more information.
 {:o}.format(0xf4)
'364'
 {:x}.format(0xf4)
'f4'
 {}.format(0xf4)
'244'

To add a prefix just put it into the format string.

You can specify the base for int() or trigger automatic base recognition by 
passing a base of 0:

 int(f4, 16)
244
 int(0xf4, 0)
244
 int(0o36, 0)
30

The classical prefix for base-8 numbers is no longer recognised in Python 3:

 int(011, 0)
Traceback (most recent call last):
  File stdin, line 1, in module
ValueError: invalid literal for int() with base 0: '011'
 int(011)
11
 int(011, 8)
9


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


Re: How to change the number into the same expression's string and vice versa?

2015-01-19 Thread Jussi Piitulainen
Peter Otten writes:
 contro opinion wrote:

  Now how to change a1,a2,a3  into b1,b2,b3 and vice versa?
  a1=0xf4
  a2=0o36
  a3=011
  
  b1='0xf4'
  b2='0o36'
  b3='011'
 
 Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
 [GCC 4.8.2] on linux
 Type help, copyright, credits or license for more information.
  {:o}.format(0xf4)
 '364'
  {:x}.format(0xf4)
 'f4'
  {}.format(0xf4)
 '244'
 
 To add a prefix just put it into the format string.

There's also these (in Python 3.2.3):

   hex(0xf4)
  '0xf4'
   oct(0xf4)
  '0o364'
   bin(0xf4)
  '0b0100'
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to change the number into the same expression's string and vice versa?

2015-01-19 Thread Frank Millman

contro opinion contropin...@gmail.com wrote in message 
news:ca+ydq_651x0ndpw1j203wgbedtxy_mw7g0w3vh1woagr1iv...@mail.gmail.com...
 In the python3 console:

 a=18
 b='18'
 str(a) == b
True
 int(b) == a
True


 Now how to change a1,a2,a3  into b1,b2,b3 and vice versa?
 a1=0xf4
 a2=0o36
 a3=011

 b1='0xf4'
 b2='0o36'
 b3='011'

I am no expert, but does this answer your question ?

C:\python
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit 
(Intel)] on win32
Type help, copyright, credits or license for more information.


 a1 = 0xf4
 a1
244
 b1 = '0x{:x}'.format(a1)
 b1
'0xf4'
 int(b1, 16)
244


 a2 = 0o36
 a2
30
 b2 = '0o{:o}'.format(a2)
 b2
'0o36'
 int(b2, 8)
30


 a3 = 011
  File stdin, line 1
a3 = 011
   ^
SyntaxError: invalid token


Frank Millman





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


Re: How to change the number into the same expression's string and vice versa?

2015-01-19 Thread Peter Otten
Jussi Piitulainen wrote:

 Peter Otten writes:

  {:o}.format(0xf4)
 '364'

 To add a prefix just put it into the format string.
 
 There's also these (in Python 3.2.3):
 
hex(0xf4)
   '0xf4'

D'oh!

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


Re: How to change the number into the same expression's string and vice versa?

2015-01-19 Thread Mark Lawrence

On 19/01/2015 09:01, contro opinion wrote:


In the python3 console:

  a=18
  b='18'
  str(a) == b
 True
  int(b) == a
 True


Now how to change a1,a2,a3  into b1,b2,b3 and vice versa?
a1=0xf4
a2=0o36
a3=011

b1='0xf4'
b2='0o36'
b3='011'




Giving a completely different take on previous answers how about.

a1, b1 = b1, a1
a2, b2 = b2, a2
a3, b3 = b3, a3

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Re: how to change the time string into number?

2014-08-20 Thread Akira Li
luofeiyu elearn2...@gmail.com writes:

 s=Aug

 how can i change it into 8 with some python time module?

  months = (None, # dummy, to start month indices from 1
  Jan,Feb,Mar,Apr,May,Jun,
  Jul,Aug,Sep,Oct,Nov,Dec
  )
  month_number = months.index(month_abbr) # month_abbr == Aug

Note:
- time.strptime(month_abbr, %b).tm_mon may fail in non-English locale
- list(calendar.month_abbr).index(month_abbr) is also locale-specific


--
Akira

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


Re: how to change the time string into number?

2014-08-15 Thread Denis McMahon
On Thu, 14 Aug 2014 14:52:17 +0800, luofeiyu wrote:

 in the manual  https://docs.python.org/3.4/library/time.html
 
 %zTime zone offset indicating a positive or negative time difference
 from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour
 digits and M represents decimal minute digits [-23:59, +23:59].
 %ZTime zone name (no characters if no time zone exists).
 
 
 t1='Sat, 09 Aug 2014  07:36:46  '
 time.strptime(t1,%a, %d %b %Y %H:%M:%S )
 time.struct_time(tm_year=2014, tm_mon=8, tm_mday=9, tm_hour=7,
 tm_min=36, tm_sec =46, tm_wday=5, tm_yday=221, tm_isdst=-1)
 
   t2='Sat, 09 Aug 2014  07:36:46  -0700' time.strptime(t2,%a, %d %b
   %Y %H:%M:%S %z)
 time.struct_time(tm_year=2014, tm_mon=8, tm_mday=9, tm_hour=7,
 tm_min=36, tm_sec =46, tm_wday=5, tm_yday=221, tm_isdst=-1)
 
 t1 and t2 is different time ,the timezone in t2 is -0700 ,why we get the
 same result?
 
   t3='Sat, 09 Aug 2014  07:36:46  +0400' time.strptime(t3,%a, %d %b
   %Y %H:%M:%S %z)
 time.struct_time(tm_year=2014, tm_mon=8, tm_mday=9, tm_hour=7,
 tm_min=36, tm_sec =46, tm_wday=5, tm_yday=221, tm_isdst=-1)
 
 
 The Directive   %z  has no any effect here,what is the matter?

Please learn to use usenet properly. Comments go below the text they 
refer to.

What version of python are you using? I know what version of the 
documentation you are looking at, but as I explained inj an earlier post, 
the implementation varies between different python versions, and for 
example python 2.7 strptime seems to completely ignore the %z in the 
format string, so again, what version of python are you using?

To check your python version:

$ python
 import sys
 sys.version

will output something like:

'2.7.3 (default, Feb 27 2014, 19:58:35) \n[GCC 4.6.3]'

for Python 2.7 or:

'3.2.3 (default, Feb 27 2014, 21:31:18) \n[GCC 4.6.3]'

for Python 3.2. Again, I stress, we need to know what version of python 
you are using to help you!

Did you run the code I posted? Did you get the same output as me? If you 
didn't, what was different. If you did get the same output, what do you 
think is wrong with it?

-- 
Denis McMahon, denismfmcma...@gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to change the time string into number?

2014-08-14 Thread luofeiyu

in the manual  https://docs.python.org/3.4/library/time.html

%z 	Time zone offset indicating a positive or negative time difference 
from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour 
digits and M represents decimal minute digits [-23:59, +23:59]. 	

%Z  Time zone name (no characters if no time zone exists).


t1='Sat, 09 Aug 2014  07:36:46  '
time.strptime(t1,%a, %d %b %Y %H:%M:%S )
time.struct_time(tm_year=2014, tm_mon=8, tm_mday=9, tm_hour=7, 
tm_min=36, tm_sec

=46, tm_wday=5, tm_yday=221, tm_isdst=-1)

 t2='Sat, 09 Aug 2014  07:36:46  -0700'
 time.strptime(t2,%a, %d %b %Y %H:%M:%S %z)
time.struct_time(tm_year=2014, tm_mon=8, tm_mday=9, tm_hour=7, 
tm_min=36, tm_sec

=46, tm_wday=5, tm_yday=221, tm_isdst=-1)

t1 and t2 is different time ,the timezone in t2 is -0700 ,why we get the 
same result?


 t3='Sat, 09 Aug 2014  07:36:46  +0400'
 time.strptime(t3,%a, %d %b %Y %H:%M:%S %z)
time.struct_time(tm_year=2014, tm_mon=8, tm_mday=9, tm_hour=7, 
tm_min=36, tm_sec

=46, tm_wday=5, tm_yday=221, tm_isdst=-1)


The Directive   %z  has no any effect here,what is the matter?

On 8/14/2014 10:01 AM, Ben Finney wrote:

luofeiyu elearn2...@gmail.com writes:


s=Aug

how can i change it into 8 with some python time module?

What is your purpose here? If you want to parse a text value into a
structured time object, don't do it piece by piece. Use the
‘time.strptime’ function.

  import time
  input_time_text = 14 Aug 2014
  input_time = time.strptime(input_text, %d %b %Y)
  input_time.tm_mon
 8



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


Re: how to change the time string into number?

2014-08-14 Thread Ben Finney
Please don't top-post your response. Instead, interleave your response
and remove irrelevant quoted material. Use the Interleaved style
URL:https://en.wikipedia.org/wiki/Posting_style#Interleaved_style.

luofeiyu elearn2...@gmail.com writes:

 in the manual  https://docs.python.org/3.4/library/time.html

 %zTime zone offset […]
 %ZTime zone name (no characters if no time zone exists).

 t1='Sat, 09 Aug 2014  07:36:46  '
 time.strptime(t1,%a, %d %b %Y %H:%M:%S )
 time.struct_time(tm_year=2014, tm_mon=8, tm_mday=9, tm_hour=7,
 tm_min=36, tm_sec
 =46, tm_wday=5, tm_yday=221, tm_isdst=-1)

Your code examples will be easier to read if you follow PEP 8 (in this
example, spaces around the operators as described in the style guide).

  t2='Sat, 09 Aug 2014  07:36:46  -0700'
  time.strptime(t2,%a, %d %b %Y %H:%M:%S %z)
 time.struct_time(tm_year=2014, tm_mon=8, tm_mday=9, tm_hour=7,
 tm_min=36, tm_sec
 =46, tm_wday=5, tm_yday=221, tm_isdst=-1)

 t1 and t2 is different time ,the timezone in t2 is -0700 ,why we get
 the same result?

The timezone in ‘t2’ will only be understood subject to the caveat:

Support for the %Z directive is based on the values contained in
tzname and whether daylight is true. Because of this, it is
platform-specific except for recognizing UTC and GMT which are
always known (and are considered to be non-daylight savings
timezones).

URL:https://docs.python.org/3/library/time.html#time.strptime

So you'll need to see what your Python implementation supports (see
‘time.tzname’).

The support for time zones is always a pain, because they *change*
rapidly, arbitrarily, and with very little warning. Because of this, the
Python standard library does not attempt to contain a timezone database,
since it would almost immediately be out of date.

Install the ‘pytz’ package to get the latest released timezone database
supported in Python URL:https://pypi.python.org/pypi/pytz.

-- 
 \  “It is better to have loft and lost than to never have loft at |
  `\   all.” —Groucho Marx |
_o__)  |
Ben Finney

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


Re: how to change the time string into number?

2014-08-14 Thread luofeiyu

 import sys
 sys.version
'3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:25:23) [MSC v.1600 64 bit 
(AMD64)]'


 import time
 time.tzname
('China Standard Time', 'China Daylight Time')


On 8/14/2014 3:25 PM, Cameron Simpson wrote:

On 14Aug2014 14:52, luofeiyu elearn2...@gmail.com wrote:

in the manual https://docs.python.org/3.4/library/time.html

┌──┬──┬─┐ 

│  │Time zone offset indicating a positive or negative time 
difference│ │
│%z│from UTC/GMT of the form +HHMM or -HHMM, where H represents 
decimal   │ │
│  │hour digits and M represents decimal minute digits [-23:59, 
+23:59].  │ │
├──┼──┼─┤ 

│%Z│Time zone name (no characters if no time zone 
exists).│ │
└──┴──┴─┘ 



t1='Sat, 09 Aug 2014  07:36:46  '
time.strptime(t1,%a, %d %b %Y %H:%M:%S )
time.struct_time(tm_year=2014, tm_mon=8, tm_mday=9, tm_hour=7, 
tm_min=36,

tm_sec
=46, tm_wday=5, tm_yday=221, tm_isdst=-1)


t2='Sat, 09 Aug 2014  07:36:46 -0700'
time.strptime(t2,%a, %d %b %Y %H:%M:%S %z)
time.struct_time(tm_year=2014, tm_mon=8, tm_mday=9, tm_hour=7, 
tm_min=36,

tm_sec
=46, tm_wday=5, tm_yday=221, tm_isdst=-1)

t1 and t2 is different time ,the timezone in t2 is -0700 ,why we get 
the same

result?


What you get back a struct_time, which is little more than the numeric 
values extracted from a time string. And as far as the text you have 
supplied in your example, those values are the same.


Regarding the difference, string in t2 has a time zone offset.

My Python 3.4 doco says (about struct_time):

  Changed in version 3.3: tm_gmtoff and tm_zone attributes are 
available on   platforms with C library supporting the corresponding 
fields in struct tm.


Judging by your output, your C library does not support the tm_gmtoff 
and tm_zone fields in its C library struct tm.


Please:

  tell us what specific version of Python you are using

  tell us what OS you're running on

Then look up the localtime() or gmtime() functions for you C library 
and see what that documentation says about struct tm, which is what 
they and the C library strptime() return.



t3='Sat, 09 Aug 2014  07:36:46 +0400'
time.strptime(t3,%a, %d %b %Y %H:%M:%S %z)
time.struct_time(tm_year=2014, tm_mon=8, tm_mday=9, tm_hour=7, 
tm_min=36,

tm_sec
=46, tm_wday=5, tm_yday=221, tm_isdst=-1)

The Directive   %z  has no any effect here,what is the matter?


The directive allows the strptime parser to keep recognising text. 
Imagine, for example, that the timezone were embedded in the middle of 
the string for some reason.


It looks like you platform does not support storing the time zone 
information in the C library struct tm, and therefore it does not 
get exposed to the Python interpreter.


Cheers,
Cameron Simpson c...@zip.com.au

What I want is Facts. Teach these boys and girls nothing but Facts.  
Facts
alone are wanted in life. Plant nothing else, and root out everything 
else.

- Charles DickensJohn Huffam   1812-1870  Hard Times [1854]


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


Re: how to change the time string into number?

2014-08-14 Thread Cameron Simpson

On 14Aug2014 15:30, luofeiyu elearn2...@gmail.com wrote:

import sys
sys.version
'3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:25:23) [MSC v.1600 64 bit 
(AMD64)]'


First, please post in an interleaved style so that we can see your responses 
underneath the text to which they relate. Thanks.


Ok, you have Python 3.4.0. And (I am guessing from the MSC), some 64 bit 
Windows install? You will need to look up the Microsoft documentation to see 
whather your C library struct tm supports timezone information.


Your Android phone will be running some flavour of Linux I believe. Someone who 
has used one may correct me here.



import time
time.tzname

('China Standard Time', 'China Daylight Time')


Ok. Have a look at time.timezone. That may help you.

Cheers,
Cameron Simpson c...@zip.com.au


On 8/14/2014 3:25 PM, Cameron Simpson wrote:

On 14Aug2014 14:52, luofeiyu elearn2...@gmail.com wrote:

in the manual https://docs.python.org/3.4/library/time.html

┌──┬──┬─┐

│  │Time zone offset indicating a positive or negative time 
difference│ │
│%z│from UTC/GMT of the form +HHMM or -HHMM, where H represents 
decimal   │ │
│  │hour digits and M represents decimal minute digits [-23:59, 
+23:59].  │ │

├──┼──┼─┤

│%Z│Time zone name (no characters if no time zone exists).
│ │

└──┴──┴─┘


t1='Sat, 09 Aug 2014  07:36:46  '
time.strptime(t1,%a, %d %b %Y %H:%M:%S )
time.struct_time(tm_year=2014, tm_mon=8, tm_mday=9, tm_hour=7, 
tm_min=36,

tm_sec
=46, tm_wday=5, tm_yday=221, tm_isdst=-1)


t2='Sat, 09 Aug 2014  07:36:46 -0700'
time.strptime(t2,%a, %d %b %Y %H:%M:%S %z)
time.struct_time(tm_year=2014, tm_mon=8, tm_mday=9, tm_hour=7, 
tm_min=36,

tm_sec
=46, tm_wday=5, tm_yday=221, tm_isdst=-1)

t1 and t2 is different time ,the timezone in t2 is -0700 ,why we 
get the same

result?


What you get back a struct_time, which is little more than the 
numeric values extracted from a time string. And as far as the text 
you have supplied in your example, those values are the same.


Regarding the difference, string in t2 has a time zone offset.

My Python 3.4 doco says (about struct_time):

 Changed in version 3.3: tm_gmtoff and tm_zone attributes are 
available on   platforms with C library supporting the corresponding 
fields in struct tm.


Judging by your output, your C library does not support the 
tm_gmtoff and tm_zone fields in its C library struct tm.


Please:

 tell us what specific version of Python you are using

 tell us what OS you're running on

Then look up the localtime() or gmtime() functions for you C library 
and see what that documentation says about struct tm, which is 
what they and the C library strptime() return.



t3='Sat, 09 Aug 2014  07:36:46 +0400'
time.strptime(t3,%a, %d %b %Y %H:%M:%S %z)
time.struct_time(tm_year=2014, tm_mon=8, tm_mday=9, tm_hour=7, 
tm_min=36,

tm_sec
=46, tm_wday=5, tm_yday=221, tm_isdst=-1)

The Directive   %z  has no any effect here,what is the matter?


The directive allows the strptime parser to keep recognising text. 
Imagine, for example, that the timezone were embedded in the middle 
of the string for some reason.


It looks like you platform does not support storing the time zone 
information in the C library struct tm, and therefore it does not 
get exposed to the Python interpreter.


Cheers,
Cameron Simpson c...@zip.com.au

What I want is Facts. Teach these boys and girls nothing but Facts.  
Facts
alone are wanted in life. Plant nothing else, and root out 
everything else.

   - Charles DickensJohn Huffam   1812-1870  Hard Times [1854]

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


Re: how to change the time string into number?

2014-08-14 Thread Cameron Simpson

On 14Aug2014 14:52, luofeiyu elearn2...@gmail.com wrote:

in the manual  https://docs.python.org/3.4/library/time.html

┌──┬──┬─┐
│  │Time zone offset indicating a positive or negative time difference│ │
│%z│from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal   │ │
│  │hour digits and M represents decimal minute digits [-23:59, +23:59].  │ │
├──┼──┼─┤
│%Z│Time zone name (no characters if no time zone exists).│ │
└──┴──┴─┘

t1='Sat, 09 Aug 2014  07:36:46  '
time.strptime(t1,%a, %d %b %Y %H:%M:%S )
time.struct_time(tm_year=2014, tm_mon=8, tm_mday=9, tm_hour=7, tm_min=36,
tm_sec
=46, tm_wday=5, tm_yday=221, tm_isdst=-1)


t2='Sat, 09 Aug 2014  07:36:46  -0700'
time.strptime(t2,%a, %d %b %Y %H:%M:%S %z)

time.struct_time(tm_year=2014, tm_mon=8, tm_mday=9, tm_hour=7, tm_min=36,
tm_sec
=46, tm_wday=5, tm_yday=221, tm_isdst=-1)

t1 and t2 is different time ,the timezone in t2 is -0700 ,why we get the same
result?


What you get back a struct_time, which is little more than the numeric values 
extracted from a time string. And as far as the text you have supplied in your 
example, those values are the same.


Regarding the difference, string in t2 has a time zone offset.

My Python 3.4 doco says (about struct_time):

  Changed in version 3.3: tm_gmtoff and tm_zone attributes are available on 
  platforms with C library supporting the corresponding fields in struct tm.


Judging by your output, your C library does not support the tm_gmtoff and 
tm_zone fields in its C library struct tm.


Please:

  tell us what specific version of Python you are using

  tell us what OS you're running on

Then look up the localtime() or gmtime() functions for you C library and see 
what that documentation says about struct tm, which is what they and the C 
library strptime() return.



t3='Sat, 09 Aug 2014  07:36:46  +0400'
time.strptime(t3,%a, %d %b %Y %H:%M:%S %z)

time.struct_time(tm_year=2014, tm_mon=8, tm_mday=9, tm_hour=7, tm_min=36,
tm_sec
=46, tm_wday=5, tm_yday=221, tm_isdst=-1)

The Directive   %z  has no any effect here,what is the matter?


The directive allows the strptime parser to keep recognising text. Imagine, for 
example, that the timezone were embedded in the middle of the string for some 
reason.


It looks like you platform does not support storing the time zone information 
in the C library struct tm, and therefore it does not get exposed to the 
Python interpreter.


Cheers,
Cameron Simpson c...@zip.com.au

What I want is Facts. Teach these boys and girls nothing but Facts.  Facts
alone are wanted in life. Plant nothing else, and root out everything else.
- Charles DickensJohn Huffam   1812-1870  Hard Times [1854]
--
https://mail.python.org/mailman/listinfo/python-list


Re: how to change the time string into number?

2014-08-14 Thread Mark Lawrence

On 14/08/2014 02:46, luofeiyu wrote:

s=Aug

how can i change it into 8 with some python time module?


If all else fails, read the instructions, so start here 
https://docs.python.org/3/library/datetime.html#module-datetime


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Re: how to change the time string into number?

2014-08-14 Thread Roy Smith
In article mailman.12994.1408021090.18130.python-l...@python.org,
 Dennis Lee Bieber wlfr...@ix.netcom.com wrote:

 On Thu, 14 Aug 2014 17:47:00 +1000, Cameron Simpson c...@zip.com.au
 declaimed the following:
 
 
 Your Android phone will be running some flavour of Linux I believe. Someone 
 who 
 has used one may correct me here.
 
   Android /is/ the flavor G
 
   Though Google has probably done some things to it that make it
 not-Linux.
 
   A closer description might be that the Android phone is running some
 flavor of Android -- since the OEMs tend to put customized skins on the
 user interface level.

The OEM marketing folks call that a product differentiator.  Most 
everybody else calls it crapware.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to change the time string into number?

2014-08-14 Thread Ben Finney
Dennis Lee Bieber wlfr...@ix.netcom.com writes:

   Android /is/ the flavor G

   Though Google has probably done some things to it that make it
 not-Linux.

Android is definitely Linux, since that is the kernel Android runs.
Remember that Linux is not an operating system; it is one part, the
kernel.

This is where it's very useful to have a distinct name to refer to the
operating system. GNU is an operating system, Android is a completely
different operating system. Both happen to have Linux as their kernel.

-- 
 \ “I went to San Francisco. I found someone's heart.” —Steven |
  `\Wright |
_o__)  |
Ben Finney

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


Re: how to change the time string into number?

2014-08-14 Thread Denis McMahon
On Thu, 14 Aug 2014 09:46:20 +0800, luofeiyu wrote:

 s=Aug
 
 how can i change it into 8 with some python time module?

You don't need a time module for this, just use a dictionary:

months = { Jan : 1, . , Dec: 12 }
num = months[s]
print num

Fill in the rest of the months dictionary yourself, it shouldn't be too 
hard.

-- 
Denis McMahon, denismfmcma...@gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to change the time string into number?

2014-08-13 Thread Chris Angelico
On Thu, Aug 14, 2014 at 11:46 AM, luofeiyu elearn2...@gmail.com wrote:
 s=Aug

 how can i change it into 8 with some python time module?

Is this homework? If not, let me set you some homework.

Step 1: Read the docs for some Python time module.
Step 2: See if it lets you do what you want.
Step 3: Return to step 1 with a different module, until your problem is solved.

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


Re: how to change the time string into number?

2014-08-13 Thread Ben Finney
luofeiyu elearn2...@gmail.com writes:

 s=Aug

 how can i change it into 8 with some python time module?

What is your purpose here? If you want to parse a text value into a
structured time object, don't do it piece by piece. Use the
‘time.strptime’ function.

 import time
 input_time_text = 14 Aug 2014
 input_time = time.strptime(input_text, %d %b %Y)
 input_time.tm_mon
8

-- 
 \“I knew it was a shocking thing to say, but … no-one has the |
  `\right to spend their life without being offended.” —Philip |
_o__)  Pullman, 2010-03-28 |
Ben Finney

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


Re: how to change the time string into number?

2014-08-13 Thread Tim Chase
On 2014-08-14 09:46, luofeiyu wrote:
 s=Aug
 
 how can i change it into 8 with some python time module?

  import time
  s = Aug
  time.strptime(s, %b).tm_mon
 8

works for me.

-tkc


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


Re: how to change the time string into number?

2014-08-13 Thread Tim Chase
On 2014-08-13 21:01, Tim Chase wrote:
 On 2014-08-14 09:46, luofeiyu wrote:
  s=Aug
  
  how can i change it into 8 with some python time module?
 
   import time
   s = Aug
   time.strptime(s, %b).tm_mon
  8
 
 works for me.

Or, if you want a more convoluted way:

  import calendar as c
  [i for i, m in enumerate(c.month_abbr) if m == Aug].pop()
 8

-tkc



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


Re: how to change the time string into number?

2014-08-13 Thread YBM

Le 14/08/2014 04:16, Tim Chase a écrit :

On 2014-08-13 21:01, Tim Chase wrote:

On 2014-08-14 09:46, luofeiyu wrote:

s=Aug

how can i change it into 8 with some python time module?


   import time
   s = Aug
   time.strptime(s, %b).tm_mon
  8

works for me.


Or, if you want a more convoluted way:

   import calendar as c
   [i for i, m in enumerate(c.month_abbr) if m == Aug].pop()
  8


it's a joke isn't it ?

 import calendar as c
 list(c.month_abbr).index('Aug')
8

BTW, why iterators does not have such an index method ?

 iter(c.month_abbr).index('Aug')
Traceback (most recent call last):
  File stdin, line 1, in module
AttributeError: 'iterator' object has no attribute 'index'

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


Re: how to change the time string into number?

2014-08-13 Thread Ian Kelly
On Wed, Aug 13, 2014 at 8:51 PM, YBM ybm...@nooos.fr.invalid wrote:
 BTW, why iterators does not have such an index method ?

Because iterators don't support indexing. In order to support such a thing,
it would have to exhaust the iterator.

 iter(range(5))[3]
Traceback (most recent call last):
  File stdin, line 1, in module
TypeError: 'range_iterator' object is not subscriptable

The only methods you can rely upon an arbitrary iterator to have are
__iter__ and __next__.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to change the time string into number?

2014-08-13 Thread Roy Smith
In article 53ec2453$0$2299$426a7...@news.free.fr,
 YBM ybm...@nooos.fr.invalid wrote:

 Le 14/08/2014 04:16, Tim Chase a écrit :
  On 2014-08-13 21:01, Tim Chase wrote:
  On 2014-08-14 09:46, luofeiyu wrote:
  s=Aug
 
  how can i change it into 8 with some python time module?
 
 import time
 s = Aug
 time.strptime(s, %b).tm_mon
8
 
  works for me.
 
  Or, if you want a more convoluted way:
 
 import calendar as c
 [i for i, m in enumerate(c.month_abbr) if m == Aug].pop()
8
 
 it's a joke isn't it ?

No, it's a song.

If I could save time in a bottle
The first thing that I'd like to do
Is to make every month be an integer number
And then I could count them with you.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to change the time string into number?

2014-08-13 Thread Marko Rauhamaa
Tim Chase python.l...@tim.thechases.com:

 Or, if you want a more convoluted way:

   import calendar as c
   [i for i, m in enumerate(c.month_abbr) if m == Aug].pop()
  8

Let's not forget the much simpler solutions:

 def eight(x): return 8
...
 eight(Aug)
8

and:

 8
8


BTW, is this a bug:

import locale
locale.getlocale()
   ('de_DE', 'UTF-8')
import time
time.strptime(Dez, %b).tm_mon
   Traceback (most recent call last):
 File stdin, line 1, in module
 File /usr/lib/python3.2/_strptime.py, line 482, in _strptime_time
   tt = _strptime(data_string, format)[0]
 File /usr/lib/python3.2/_strptime.py, line 337, in _strptime
   (data_string, format))
   ValueError: time data 'Dez' does not match format '%b'
time.strftime(%b, time.localtime(time.time() + 120 * 86400))
   'Dec'
time.strftime(%x)
   '08/14/14'

After all, %b is documented as Locale’s abbreviated month name.

Anyway, %b *should* depend on the locale, so str[pf]time may not be
suitable to deal with email dates, for example.


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


Re: how to change system-wide proxy settings by Python ?

2013-02-03 Thread Kwpolska
On Sun, Feb 3, 2013 at 4:34 PM, iMath redstone-c...@163.com wrote:
 I have already known a valid proxy server(63.141.216.159)and port(8087) which 
 support both http and https protocols ,so how to change system-wide proxy 
 settings to this proxy by Python ?
 I use WinXP ,can you show  me an example of this ?
 thanks in advance !
 --
 http://mail.python.org/mailman/listinfo/python-list

This may help you:

http://stackoverflow.com/questions/1068212/programmatically-detect-system-proxy-settings-on-windows-xp-with-python

Next time, please use Google before you ask.
-- 
Kwpolska http://kwpolska.tk | GPG KEY: 5EAAEA16
stop html mail| always bottom-post
http://asciiribbon.org| http://caliburn.nl/topposting.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to change system-wide proxy settings by Python ?

2013-02-03 Thread Michael Torrie
On 02/03/2013 08:34 AM, iMath wrote:
 I have already known a valid proxy server(63.141.216.159)and
 port(8087) which support both http and https protocols ,so how to
 change system-wide proxy settings to this proxy by Python ? I use
 WinXP ,can you show  me an example of this ? thanks in advance !

There really is no way on any operating system to set a system-wide
proxy that is honored by every program that does http.

However if you can change the one Internet Settings proxy
programmatically, any windows app that use the IE browser engine will
pick it up.  One method to do this is to interact with the registry.
You can google for the appropriate key.  Setting it for all users,
though, is a bit trickier.  Your script would need privileges to access
keys in HKEY_LOCAL_MACHINE.

But be warned that other programs like firefox and Chrome will not
automatically know about this setting or honor it.  Or any program that
implements its own http requests with sockets.  It's not something that
can be enforced as a sort of policy.  If you need that kind of
enforcing, you'll have to work with the network hardware to block
un-proxied http and https traffic.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to change colors of multiple widgets after hovering in Tkinter

2013-01-10 Thread Peter Otten
mountdoo...@gmail.com wrote:

 I´m trying to make a script, which will change the background and
 foreground color of widgets after hovering.

 but when I hover on any button, nothing happens, they stay white. I know I
 could use a function, but there would be two functions for every widget (1
 for , 1 for ). I'd like to create a single function, which will recolor
 that widget I hover on and explain why this script is not doing what I
 want it to do.
 
 I hope I described my problem well. 

You did.

 from Tkinter import *
 
 root=Tk()
 
 Hover1=Button(root,text=Red color, bg=white)
 Hover1.pack()
 
 Hover2=Button(root,text=Yellow color, bg=white)
 Hover2.pack()
 
 Hover1.bind(Enter,Hover1.configure(bg=red))

This calls Hover1.configure(bg=red) once and binds the result of that 
method call (which is None) to the event. So the above line is equivalent to

Hover1.configure(bg=red)
Hover1.bind(Enter, None)

You say you don't want to write a function, but that is really the correct 
aproach. Fortunately there is a way to create such a function on the fly:

def f(event):
Hover1.configure(bg=red)

can be written as

f = lambda event: Hover1.configure(bg=red)

With that your code becomes

Hover1.bind(Enter, lambda event: Hover1.configure(bg=red))
Hover1.bind(Leave, lambda event: Hover1.configure(bg=white))

and so on. In this specific case this doesn't have the desired effect 
because when the mouse enters a Button widget its background color changes 
to 'activebackground'. So you don't really need to bind the enter/leave 
events. Specify an activebackground instead when you create the buttons. For 
example:

Hover1 = Button(root, text=Red color, bg=white, activebackground=red)
Hover1.pack()



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


Re: How to change colors of multiple widgets after hovering in Tkinter

2013-01-10 Thread Rick Johnson
On Thursday, January 10, 2013 1:13:38 PM UTC-6, Peter Otten wrote:
 mountdoom wrote:
  I´m trying to make a script, which will change the background and
  foreground color of widgets after hovering.

Peter's advice is spot on except you may want ALL widgets to change colors on 
ENTER and LEAVE events. If you want all widgets use the w.bind_all method 
instead of w.bind. Also check out the w.bind_class method to confine 
bindings to one particular class of widget (like a Tkinter.Button).
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: how to change os.popen4 to subprocess

2012-10-30 Thread Prasad, Ramit
Replying to skyworld because I could not find the original message
from MRAB.

skyworld wrote:
 On Oct 27, 11:02 am, MRAB pyt...@mrabarnett.plus.com wrote:
  On 2012-10-27 03:28, skyworld wrote: Hi,
 
   I'm new to python and I'm trying to porting some scripts from v0.96 to
   v2.0.1. A piece of code is like this:
 
   cmd_h = os.popen4(env['SYSCMDLINE'])[1]
 
   the system indicates the popen4 is deprecated and suggest to use
   subprocess. Can anybody tell me how to use subprocess in this case?
   and what does [1] here means?
 
  os.popen4 returns a tuple of (child_stdin, child_stdout_and_stderr).
  The [1] gets the child_stdout_and_stderr member.
 
  Using the subprocess module:
 
  # Untested!
  cmd_h = subprocess.Popen(env['SYSCMDLINE'], stdout=subprocess.PIPE,
  stderr=subprocess.STDOUT, shell=True).stdout
 
  Explanation:
 
  The command line: env['SYSCMDLINE']
 
  Return stdout: stdout=subprocess.PIPE
 
  stderr should be combined with stdout: stderr=subprocess.STDOUT
 
  Let the shell parse the command line: shell=True
 
 thanks
 --

I thought the usage of shell=True is usually discouraged? The 
subprocess documentation[0] should be helpful to figure it out.

Warning: Invoking the system shell with shell=True can be a security 
hazard if combined with untrusted input. See the warning under 
Frequently Used Arguments for details.


[0] http://docs.python.org/2/library/subprocess.html 


Ramit


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to change os.popen4 to subprocess

2012-10-27 Thread skyworld
On Oct 27, 11:02 am, MRAB pyt...@mrabarnett.plus.com wrote:
 On 2012-10-27 03:28, skyworld wrote: Hi,

  I'm new to python and I'm trying to porting some scripts from v0.96 to
  v2.0.1. A piece of code is like this:

  cmd_h = os.popen4(env['SYSCMDLINE'])[1]

  the system indicates the popen4 is deprecated and suggest to use
  subprocess. Can anybody tell me how to use subprocess in this case?
  and what does [1] here means?

 os.popen4 returns a tuple of (child_stdin, child_stdout_and_stderr).
 The [1] gets the child_stdout_and_stderr member.

 Using the subprocess module:

 # Untested!
 cmd_h = subprocess.Popen(env['SYSCMDLINE'], stdout=subprocess.PIPE,
 stderr=subprocess.STDOUT, shell=True).stdout

 Explanation:

 The command line: env['SYSCMDLINE']

 Return stdout: stdout=subprocess.PIPE

 stderr should be combined with stdout: stderr=subprocess.STDOUT

 Let the shell parse the command line: shell=True

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


Re: how to change os.popen4 to subprocess

2012-10-26 Thread MRAB

On 2012-10-27 03:28, skyworld wrote:

Hi,

I'm new to python and I'm trying to porting some scripts from v0.96 to
v2.0.1. A piece of code is like this:

cmd_h = os.popen4(env['SYSCMDLINE'])[1]

the system indicates the popen4 is deprecated and suggest to use
subprocess. Can anybody tell me how to use subprocess in this case?
and what does [1] here means?


os.popen4 returns a tuple of (child_stdin, child_stdout_and_stderr).
The [1] gets the child_stdout_and_stderr member.

Using the subprocess module:

# Untested!
cmd_h = subprocess.Popen(env['SYSCMDLINE'], stdout=subprocess.PIPE, 
stderr=subprocess.STDOUT, shell=True).stdout


Explanation:

The command line: env['SYSCMDLINE']

Return stdout: stdout=subprocess.PIPE

stderr should be combined with stdout: stderr=subprocess.STDOUT

Let the shell parse the command line: shell=True

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


Re: how to change os.popen4 to subprocess

2012-10-26 Thread Mark Lawrence

On 27/10/2012 03:28, skyworld wrote:

Hi,

I'm new to python and I'm trying to porting some scripts from v0.96 to
v2.0.1. A piece of code is like this:


What software are you talking about here, it's certainly not Python 
versions as the most up to date are 2.7.3 and 3.3.0?




cmd_h = os.popen4(env['SYSCMDLINE'])[1]

the system indicates the popen4 is deprecated and suggest to use
subprocess. Can anybody tell me how to use subprocess in this case?
and what does [1] here means?


If you don't know what the [1] means you've got problems :)  I suggest 
you read the tutorial here first 
http://docs.python.org/tutorial/index.html then the subprocess module 
here http://docs.python.org/library/subprocess.html#module-subprocess, 
specifically 
http://docs.python.org/library/subprocess.html#subprocess-replacements




thanks.



No problem.

--
Cheers.

Mark Lawrence.

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


Re: How to change the file creation timestamp?

2011-11-25 Thread Alec Taylor
import os
import time
from stat import *

#returns a list of all the files on the current directory
files = os.listdir('.')

for f in files:
  #my folder has some jpegs and raw images
  if f.lower().endswith('jpg') or f.lower().endswith('crw'):
st = os.stat(f)
atime = st[ST_ATIME] #access time
mtime = st[ST_MTIME] #modification time

new_mtime = mtime + (4*3600) #new modification time

#modify the file timestamp
os.utime(f,(atime,new_mtime))

On Fri, Nov 25, 2011 at 11:47 PM, 刘振海 1989l...@gmail.com wrote:
 Hi,
 I want to change the file creation timestamp using python, but I can not
 find a solution to do it.
 I know the way to change the file creation timestamp in C under Windows, but
 I want to change it using python.
 I really need help!

 Regards,
 Liu Zhenhai

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

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


Re: How to change the file creation timestamp?

2011-11-25 Thread 刘振海
Hi Alec
Thanks for your help.

I want to change the creation timestamp. the code that you give is to
change the modification and access time.
I already find a solution using pywin32's win32file module

import win32file
filehandle = win32file.CreateFile(file_name, win32file.GENERIC_WRITE,
0, None, win32file.OPEN_EXISTING, 0, 0)
win32file.SetFileTime(filehandle, ctime, atime, mtime)

Regards,
Liu Zhenhai

2011/11/25 Alec Taylor alec.tayl...@gmail.com

 import os
 import time
 from stat import *

 #returns a list of all the files on the current directory
 files = os.listdir('.')

 for f in files:
  #my folder has some jpegs and raw images
  if f.lower().endswith('jpg') or f.lower().endswith('crw'):
st = os.stat(f)
atime = st[ST_ATIME] #access time
mtime = st[ST_MTIME] #modification time

new_mtime = mtime + (4*3600) #new modification time

#modify the file timestamp
os.utime(f,(atime,new_mtime))

 On Fri, Nov 25, 2011 at 11:47 PM, 刘振海 1989l...@gmail.com wrote:
  Hi,
  I want to change the file creation timestamp using python, but I can not
  find a solution to do it.
  I know the way to change the file creation timestamp in C under Windows,
 but
  I want to change it using python.
  I really need help!
 
  Regards,
  Liu Zhenhai
 
  --
  http://mail.python.org/mailman/listinfo/python-list
 

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


Re: How to change the file creation timestamp?

2011-11-25 Thread Steven D'Aprano
On Sat, 26 Nov 2011 00:51:34 +1100, Alec Taylor wrote:

 import os
 import time
 from stat import *
 
 #returns a list of all the files on the current directory files =
 os.listdir('.')
 
 for f in files:
   #my folder has some jpegs and raw images if f.lower().endswith('jpg')
   or f.lower().endswith('crw'):
 st = os.stat(f)
 atime = st[ST_ATIME] #access time
 mtime = st[ST_MTIME] #modification time

The original poster asks for how to change the file creation timestamp. 
(The poster assumes that there is a creation timestamp, which is not 
necessarily the case -- many file systems do not store the creation time.)


 new_mtime = mtime + (4*3600) #new modification time
 
 #modify the file timestamp
 os.utime(f,(atime,new_mtime))

Note that this is from the posix module, so it probably won't work under 
Windows.


 
 On Fri, Nov 25, 2011 at 11:47 PM, 刘振海 1989l...@gmail.com wrote:
 Hi,
 I want to change the file creation timestamp using python, but I can
 not find a solution to do it.
 I know the way to change the file creation timestamp in C under
 Windows, but I want to change it using python.
 I really need help!

 Regards,
 Liu Zhenhai

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


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


Re: how to change the order of a button, static text or other components

2011-10-21 Thread Ian Kelly
I assume you're arranging the components with a sizer.  Remove them from the
sizer, reinsert them in the order you want, and then call sizer.Layout().

Cheers,
Ian
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: how to change the order of a button, static text or other components

2011-10-21 Thread Prasad, Ramit
what i want to do is,when i press a button, i change the order of
selected components,how to do this?

This is so vague, I am tempted to think it is spambut on the chance it is 
not, I need a lot more information than you are providing to even attempt to 
give you any guidance.

1. By button do you mean keyboard/mouse or GUI; if you mean GUI button then 
what toolkit (Tk, wx, etc) are you referring to?
2. Define selected components. How are they selected? What is a component?
3. Define order in regards to components.

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to change the order of a button, static text or other components

2011-10-20 Thread Chris Rebert
On Thu, Oct 20, 2011 at 6:08 PM, install...@189.cn install...@189.cn wrote:
 what i want to do is,when i press a button, i change the order of
 selected components,how to do this?

Which GUI toolkit are you using?

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to change the order of a button, static text or other components

2011-10-20 Thread install...@189.cn
On 10月21日, 上午9时26分, Chris Rebert c...@rebertia.com wrote:
 On Thu, Oct 20, 2011 at 6:08 PM, install...@189.cn install...@189.cn wrote:
  what i want to do is,when i press a button, i change the order of
  selected components,how to do this?

 Which GUI toolkit are you using?

 Cheers,
 Chris

wxpython.
thx so much.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to change a string into dictionary

2010-08-09 Thread Shashwat Anand
On Mon, Aug 9, 2010 at 3:03 PM, aimeixu aime...@amazon.com wrote:

 Hi,
 I am newbie for python ,Here is my question:
 a = {'a':'1','b':'2'}
 how to change a into a dictionary ,says, a = {'a':'1','b':'2'}
 Thanks a lot .Really need help.


Parse the string and re-create the dictionary.

 s = {'a':'1','b':'2'}
 ds = {}
 for i in s.strip('{}').split(','):
... key, val = i.split(':')
... ds[key.strip(')] = val.strip(')
...
 ds
{'a': '1', 'b': '2'}
 type(ds)
class 'dict'


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




-- 
~l0nwlf
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to change a string into dictionary

2010-08-09 Thread Chris Rebert
On Mon, Aug 9, 2010 at 2:53 AM, Shashwat Anand anand.shash...@gmail.com wrote:
 On Mon, Aug 9, 2010 at 3:03 PM, aimeixu aime...@amazon.com wrote:
 Hi,
 I am newbie for python ,Here is my question:
 a = {'a':'1','b':'2'}
 how to change a into a dictionary ,says, a = {'a':'1','b':'2'}
 Thanks a lot .Really need help.

 Parse the string and re-create the dictionary.
 s = {'a':'1','b':'2'}
 ds = {}
 for i in s.strip('{}').split(','):
 ...     key, val = i.split(':')
 ...     ds[key.strip(')] = val.strip(')

Just for the record, that'll break if the dictionary entries have
embedded commas or colons.
eval() will handle such cases correctly and probably* run faster, but
is obviously insecure.

If you have control of both ends of the serialization process, you
might consider using the `json` or `pickle` std lib modules instead.

Cheers,
Chris
--
*Do a benchmark obviously.
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to change a string into dictionary

2010-08-09 Thread Ulrich Eckhardt
aimeixu wrote:
 a = {'a':'1','b':'2'}
 how to change a into a dictionary ,says, a = {'a':'1','b':'2'}

You could evaluate it as regular Python code, using exec:

  res = {}
  exec(a={'a':'1'}, res)
  print res['a']

However, if this is input from a file or the user, be aware that this opens
loopholes for executing _any_ code, so you should only exec code from
sources you can trust.

Uli

-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

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


Re: how to change a string into dictionary

2010-08-09 Thread Daniel Urban
 a = {'a':'1','b':'2'}
 how to change a into a dictionary ,says, a = {'a':'1','b':'2'}

See also the ast.literal_eval function:
http://docs.python.org/py3k/library/ast.html#ast.literal_eval


Daniel
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to change when the logging module creates the log file?

2010-01-06 Thread Chris Colbert
i was able to fix the exception by calling logging.shutdown() before the
call to os.remove().

However, I still think there is probably a more elegant solution.

On Wed, Jan 6, 2010 at 12:57 PM, Chris Colbert sccolb...@gmail.com wrote:

 I have an application the writes to a log file when specific exceptions are
 handled. However, if no exceptions are encountered, I don't want to create a
 log at all.

 The problem I am running into is that the stdlib logging module creates the
 log file immediately upon logger instantiation.

 Thus:
  logger = logging.basicConifg('testlog.txt')

 already creates the file 'testlog.txt'.

 So, at program close, I am reading the size of the log file, and if it is
 empty I remove it with os.remove(). This works fine on Linux, but throws a
 permission denied exception on Windows.

 There has to be a better way to do this than using a hack like that. Is
 there a way to make the logging module hold-off on file creation until the
 first log is generated? I could do it by wrapping logger in a class, but
 that would remove the beauty of having any module import logging from the
 stdlib and being able to write to the log.

 Thanks for any pointers!

 Chris

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


Re: How to change string or number passed as argument?

2009-09-21 Thread Gabriel Genellina

En Sat, 19 Sep 2009 22:59:21 -0300, Peng Yu pengyu...@gmail.com escribió:


I know that strings or numbers are immutable when they passed as
arguments to functions. But there are cases that I may want to change
them in a function and propagate the effects outside the function. I
could wrap them in a class, which I feel a little bit tedious. I am
wondering what is the common practice for this problem.


In addition to all previous responses: Sometimes, you have a function that
should return more than one piece of information. On other languages, you
have to choose *one* of them as *the* function return value, and the
others become out parameters. In Python you simply return all of them:

def decode_index(index):
  convert linear index into row, col coordinates
  return index // width, index % width # divmod would be better...

row, col = decode_index(index)

(Tecnically, you're still returning ONE object - a tuple. But since
packing and unpacking of values is done automatically, you may consider it
as returning multiple values at the same time).

--
Gabriel Genellina

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


Re: How to change string or number passed as argument?

2009-09-20 Thread Hendrik van Rooyen
On Sunday 20 September 2009 03:59:21 Peng Yu wrote:

 I know that strings or numbers are immutable when they passed as
 arguments to functions. But there are cases that I may want to change
 them in a function and propagate the effects outside the function. I
 could wrap them in a class, which I feel a little bit tedious. I am
 wondering what is the common practice for this problem.

You can just ignore the immutability.
Nothing stops you doing something like this;

def reader(port,buffer):
buffer += port.read()
return buffer

and calling it repetitively until buffer is as long as you want it.

- Hendrik

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


Re: How to change string or number passed as argument?

2009-09-20 Thread Simon Forman
On Sep 19, 9:59 pm, Peng Yu pengyu...@gmail.com wrote:
 Hi,

 I know that strings or numbers are immutable when they passed as
 arguments to functions. But there are cases that I may want to change
 them in a function and propagate the effects outside the function. I
 could wrap them in a class, which I feel a little bit tedious. I am
 wondering what is the common practice for this problem.

 Regards,
 Peng

Python strings and numbers are always immutable, not just when passed
as arguments.

propagate the effects outside the function is a little vague.

You can return new data objects (like str.lower() etc.. do) or you can
wrap them in a namespace (a dict or class instance) or you can pass a
list object that contains the string or int or whatever, and your
functions can replace the values in the dict/instance/list.

HTH,
~Simon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to change string or number passed as argument?

2009-09-19 Thread Tim Chase

I know that strings or numbers are immutable when they passed as
arguments to functions. But there are cases that I may want to change
them in a function and propagate the effects outside the function. I
could wrap them in a class, which I feel a little bit tedious. I am
wondering what is the common practice for this problem.


The most common way is to simply return the altered string if you 
need it:


  def my_func(some_string):
result = do_stuff(...)
some_string = mutate(some_string)
return result, some_string

  result, value = my_func(value)

This gives the flexibility for the caller to decide whether they 
want to allow the function to mutate the parameter or not.



You can also use a mutable argument:

  def my_func(lst):
lst[0] = mutate(lst[0])
return do_stuff(...)
  s = [hello]
  result = my_func(s)
  print s[0]

but this is horribly hackish.

In general, mutating arguments is frowned upon because it leads 
to unexpected consequences.  Just like I don't expect sin(x) or 
cos(x) to go changing my input value, python functions should 
behave similarly.


-tkc



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


Re: how to change response code in CGIHTTPServer.py

2009-05-29 Thread Jeff McNeil
On May 28, 2:23 pm, Daniel daniel.watr...@gmail.com wrote:
 Hello,

 Python 2.5.2
 WinXP

 I'm using CGIHTTPServer.py and want to return a response code of 400
 with a message in the event that the cgi script fails for some
 reason.  I notice that
 run_cgi(self):
 executes this line of code,
 self.send_response(200, Script output follows)
 which overwrites any headers that I print in my cgi.  Is there some
 way to modify the response code without having to override
 CGIHTTPServer.py?

 Thanks,

 help (CGIHTTPServer)
...
Note that status code 200 is sent prior to execution of a CGI script,
so
scripts cannot send other status codes such as 302 (redirect).
...


It sets that header before it even fires off the CGI script and just
pipes the following response to the client.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to change a generator ?

2008-12-24 Thread MRAB

Barak, Ron wrote:

Hi,
 
I have a generator whose aim is to returns consecutive lines from a file 
(the listing below is a simplified version).
However, as it is written now, the generator method changes the text 
file pointer to end of file after first invocation.

Namely, the file pointer changes from 0 to 6623 on line 24.
 
It might be that the generator method of self.input_file is reading the 
file a chunk at a time for efficiency even though it's yielding a line 
at a time.


Can you suggest how the generator could be changed, so it will allow me 
to get the current location in the file after each yield ?
 
Thanks,

Ron.
 
$ cat -n generator.py  # listing without line numbers is below

 1  #!/usr/bin/env python
 2
 3  import gzip
 4  from Debug import _line as line
 5
 6  class LogStream():
 7 
 8  def __init__(self, filename):

 9  self.filename = filename
10  self.input_file = self.open_file(filename)
11
12  def open_file(self, in_file):
13  try:
14  f = gzip.GzipFile(in_file, r)
15  f.readline()
16  except IOError:
17  f = open(in_file, r)
18  f.readline()
19  f.seek(0)
20  return(f)
21
22  def line_generator(self):
23  print line()+. 
self.input_file.tell()==,self.input_file.tell()

24  for line_ in self.input_file:
25  print line()+. 
self.input_file.tell()==,self.input_file.tell()

26  yield line_.strip()
27
28
29  if __name__ == __main__:
30
31  filename = sac.log.50lines
32  log_stream = LogStream(filename)
33  log_stream.input_file.seek(0)
34  line_generator = log_stream.line_generator()
35  line_ = line_generator.next()
 
$ python generator.py

23. self.input_file.tell()== 0
25. self.input_file.tell()== 6623
 
$ wc -c sac.log.50lines

6623 sac.log.50lines
$ cat generator.py
#!/usr/bin/env python
 
import gzip

from Debug import _line as line
 
class LogStream():
   
def __init__(self, filename):

self.filename = filename
self.input_file = self.open_file(filename)
 
def open_file(self, in_file):

try:
f = gzip.GzipFile(in_file, r)
f.readline()
except IOError:
f = open(in_file, r)
f.readline()
f.seek(0)
return(f)
 
def line_generator(self):

print line()+. self.input_file.tell()==,self.input_file.tell()
for line_ in self.input_file:
print line()+. self.input_file.tell()==,self.input_file.tell()
yield line_.strip()
 


if __name__ == __main__:
 
filename = sac.log.50lines

log_stream = LogStream(filename)
log_stream.input_file.seek(0)
line_generator = log_stream.line_generator()
line_ = line_generator.next()
 

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


Re: How to change a generator ?

2008-12-24 Thread Gabriel Genellina
En Wed, 24 Dec 2008 15:03:58 -0200, MRAB goo...@mrabarnett.plus.com  
escribió:


 I have a generator whose aim is to returns consecutive lines from a  
file (the listing below is a simplified version).
However, as it is written now, the generator method changes the text  
file pointer to end of file after first invocation.

Namely, the file pointer changes from 0 to 6623 on line 24.

It might be that the generator method of self.input_file is reading the  
file a chunk at a time for efficiency even though it's yielding a line  
at a time.


I think this is the case too.
I can think of 3 alternatives:

a) open the file unbuffered (bufsize=0). But I think this would greatly  
decrease performance.


b) keep track internally of file position (by adding each line length).  
The file should be opened in binary mode in this case (to avoid any '\n'  
translation).


c) return line numbers only, instead of file positions. Seeking to a  
certain line number requires to re-read the whole file from start;  
depending on how often this is required, and how big is the file, this  
might be acceptable.


--
Gabriel Genellina

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


Re: How to change a generator ?

2008-12-24 Thread MRAB

Gabriel Genellina wrote:
En Wed, 24 Dec 2008 15:03:58 -0200, MRAB goo...@mrabarnett.plus.com 
escribió:


 I have a generator whose aim is to returns consecutive lines from a 
file (the listing below is a simplified version).
However, as it is written now, the generator method changes the text 
file pointer to end of file after first invocation.

Namely, the file pointer changes from 0 to 6623 on line 24.

It might be that the generator method of self.input_file is reading 
the file a chunk at a time for efficiency even though it's yielding a 
line at a time.


I think this is the case too.
I can think of 3 alternatives:

a) open the file unbuffered (bufsize=0). But I think this would greatly 
decrease performance.


b) keep track internally of file position (by adding each line length). 
The file should be opened in binary mode in this case (to avoid any '\n' 
translation).


c) return line numbers only, instead of file positions. Seeking to a 
certain line number requires to re-read the whole file from start; 
depending on how often this is required, and how big is the file, this 
might be acceptable.


readline() appears to work as expected, leaving the file position at the 
start of the next line.

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


RE: How to change a generator ? - resolved

2008-12-24 Thread Barak, Ron
Hi  Gabriel,

Your remarks fixed my problem. Now my code looks as below, and behaves as 
expected.

Thanks Gabriel.

Merry Christmas and Happy Hanukkah,
Ron.


$ cat generator.py
#!/usr/bin/env python

import gzip
from Debug import _line as line

class LogStream():

def __init__(self, filename):
self.filename = filename
self.input_file = self.open_file(filename)

def open_file(self, in_file):
try:
f = gzip.GzipFile(in_file, r)
f.readline()
except IOError:
f = open(in_file, r)
f.readline()
f.seek(0)
return(f)

def line_generator(self):
print line()+. self.input_file.tell()==,self.input_file.tell()
while True:
line_ = self.input_file.readline()
print line()+. self.input_file.tell()==,self.input_file.tell()
if not line_:
break
yield line_.strip()


if __name__ == __main__:

filename = sac.log.50lines
log_stream = LogStream(filename)
log_stream.input_file.seek(0)
line_generator = log_stream.line_generator()
line_ = line_generator.next()

$ python generator.py
23. self.input_file.tell()== 0
26. self.input_file.tell()== 247

$ !wc
wc -c sac.log.50lines
6623 sac.log.50lines

$

-Original Message-
From: MRAB [mailto:goo...@mrabarnett.plus.com]
Sent: Wednesday, December 24, 2008 20:00
To: python-list@python.org
Subject: Re: How to change a generator ?

Gabriel Genellina wrote:
 En Wed, 24 Dec 2008 15:03:58 -0200, MRAB goo...@mrabarnett.plus.com
 escribió:

  I have a generator whose aim is to returns consecutive lines from a
 file (the listing below is a simplified version).
 However, as it is written now, the generator method changes the text
 file pointer to end of file after first invocation.
 Namely, the file pointer changes from 0 to 6623 on line 24.

 It might be that the generator method of self.input_file is reading
 the file a chunk at a time for efficiency even though it's yielding a
 line at a time.

 I think this is the case too.
 I can think of 3 alternatives:

 a) open the file unbuffered (bufsize=0). But I think this would
 greatly decrease performance.

 b) keep track internally of file position (by adding each line length).
 The file should be opened in binary mode in this case (to avoid any '\n'
 translation).

 c) return line numbers only, instead of file positions. Seeking to a
 certain line number requires to re-read the whole file from start;
 depending on how often this is required, and how big is the file, this
 might be acceptable.

readline() appears to work as expected, leaving the file position at the start 
of the next line.

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


Re: how to change current working directory while using pdb within emacs

2007-11-26 Thread du yan ning
On Nov 21, 1:28 pm, Diez B. Roggisch [EMAIL PROTECTED] wrote:
 duyanningwrote:
  I have written a pyhton script that will process data file in current
  working directory.
  My script is in an different directory to data file.
  When I debug this script using pdb within emacs, emacs will change the
  current working directory to the directory which include the script,
  so my script cannot find the data file.

  I think this is the problem of emacs because when I start pdb from
  console directly, it will not change current working directory to the
  one of script being debugged.

 Just issue

 import os
 os.chdir('whatever')

 inside the pdb-session. Unfortunate, but should work.

 Diez

thank you! my friend.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to change current working directory while using pdb within emacs

2007-11-26 Thread R. Bernstein
duyanning [EMAIL PROTECTED] writes:

 I have written a pyhton script that will process data file in current
 working directory.
 My script is in an different directory to data file.
 When I debug this script using pdb within emacs, emacs will change the
 current working directory to the directory which include the script,
 so my script cannot find the data file.
 
 I think this is the problem of emacs because when I start pdb from
 console directly, it will not change current working directory to the
 one of script being debugged.
 
 please help me.
 thank you.

pydb (http://bashdb.sf.net/pydb) has an option to set the current
working directory on invocation. For example, from emacs I can run:

M-x pydb --cd=/tmp --annotate=3 ~/python/hanoi.py 3 

And then when I type 
  import os; os.getcwd()

I get:
  '/tmp'

Inside pydb, there is a cd command saving you the trouble of
importing os; the directory command can be used to set a search path
for source files. All same as you would do in gdb.

Begin digression ---

The --annotate option listed above, is similar to gdb's annotation
mode. It is a very recent addition and is only in CVS. With annotation
mode turned on, the effect is similar to running gdb (gdb-ui.el) in
Emacs 22.  Emacs internal buffers track the state of local variables,
breakpoints and the stack automatically as the code progresses. If the
variable pydb-many-windows is set to true, the each of these buffers
appear in a frame.

For those of you who don't start pydb initially bug enter via a call
such as set_trace() or debugger() and do this inside an Emacs comint
shell with pydb-tracking turned on, issue set annotation 3 same as
you would if you were in gdb.

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


Re: how to change current working directory while using pdb within emacs

2007-11-21 Thread Diez B. Roggisch
duyanning wrote:

 I have written a pyhton script that will process data file in current
 working directory.
 My script is in an different directory to data file.
 When I debug this script using pdb within emacs, emacs will change the
 current working directory to the directory which include the script,
 so my script cannot find the data file.
 
 I think this is the problem of emacs because when I start pdb from
 console directly, it will not change current working directory to the
 one of script being debugged.

Just issue

import os
os.chdir('whatever')

inside the pdb-session. Unfortunate, but should work.

Diez


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


Re: how could change backcolor of console?

2007-09-15 Thread buffi
On Sep 15, 3:55 pm, [EMAIL PROTECTED] wrote:
   Hi,everyone: I am a c programmer,and want using Python instead of C
 I  can  change backcolor using  C,like this:

  textbackground(color);

 How can I do in Python?

 Best regards

 点 击 此 处!免 费 试 玩 07 年 最 受 期 待 的 游 戏 大 作 !

textbackground() is not part of the C programming language but rather
a part of an rather old module for msdos.
http://en.wikipedia.org/wiki/Conio.h

If you want terminal colors in python you can use the curses module
but I doubt that it will work in windows.
http://docs.python.org/lib/module-curses.html

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

Re: How to change a PyObject passed to the C extension

2007-08-11 Thread Marc 'BlackJack' Rintsch
On Sat, 11 Aug 2007 09:43:19 -0700, MD wrote:

I have a Python C extension which is passed a PyObject containing
 an integer value. Is it possible to change this same PyObject so that
 now the integer is of a different value?

No it is not.  Even if you poke around in the object ``struct`` this would
have severe consequences for cached/shared objects.  Just imagine:

from your_module import bzzzt

def f():
print 2 + 2

bzzzt(2)   # This changes the value of 2 to 3.
f()# This prints '6'!

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to change menu text with Tkinter?

2006-10-01 Thread Scott David Daniels
Phil Schmidt wrote:
 Eric Brunel wrote:
 But Marc's answer still applies: it's a lot of work for something that
 will usually be configured once. So requiring to restart the tool when the
 UI language changes should be acceptable.
 
 Thanks for the example, that helps.
 
 I agree with you and Marc regarding the language configuration method.
 The requirements aren't mine however - my customer wants the language
 selectable at runtime, so I'm kind of stuck with that.

You might also explain to the customer that of any menu elements are
ordered alphabetically, the result of changing the language will be
jarring to the user, as well as expensive to implement.

--Scott David Daniels
[EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to change menu text with Tkinter?

2006-09-27 Thread Marc 'BlackJack' Rintsch
In [EMAIL PROTECTED], Phil Schmidt
wrote:

 I am making a little Tkinter GUI app that needs to be in several
 languages (english, french, etc.), adjustable at runtime via a menu
 pick to select the language. The only way I can see to change text in
 the menus entries is to destroy them and recreate them usiing different
 labels. This seems very clunky though, and there must be a better way.
 Can anyone offer any pointers or a short example for how to do this?

Do you really need this at runtime or is it enough to store the language
in a config file and ask the user to restart the application?  Then using
the `gettext` module is a good way.  Maybe in combination with the
`locale` module so you can use the language of the operating system as
default if the user doesn't choose one explicitly.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to change menu text with Tkinter?

2006-09-27 Thread Rob Wolfe

Phil Schmidt wrote:
 I am making a little Tkinter GUI app that needs to be in several
 languages (english, french, etc.), adjustable at runtime via a menu
 pick to select the language. The only way I can see to change text in
 the menus entries is to destroy them and recreate them usiing different
 labels. This seems very clunky though, and there must be a better way.
 Can anyone offer any pointers or a short example for how to do this?

Try this:

menu.entryconfig(index, label=new_name)

HTH,
Rob

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


Re: How to change menu text with Tkinter?

2006-09-27 Thread Eric Brunel
On Wed, 27 Sep 2006 15:29:32 +0200, Phil Schmidt  
[EMAIL PROTECTED] wrote:

 I am making a little Tkinter GUI app that needs to be in several
 languages (english, french, etc.), adjustable at runtime via a menu
 pick to select the language. The only way I can see to change text in
 the menus entries is to destroy them and recreate them usiing different
 labels. This seems very clunky though, and there must be a better way.
 Can anyone offer any pointers or a short example for how to do this?

Here is a way:

--
 from Tkinter import *

root = Tk()
mb = Menu(root)
root.configure(menu=mb)
fm = Menu(mb)
mb.add_cascade(label='File', menu=fm)

def chlg():
   mb.entryconfigure(1, label='Fichier')
   fm.entryconfigure(1, label='Changer de langue')
   fm.entryconfigure(2, label='Quitter')

fm.add_command(label='Change language', command=chlg)
fm.add_command(label='Quit', command=root.quit)

root.mainloop()
--

Note that the entry indices start at 1 because of the 'tearoff' entry that  
is always created in a menu. If you specify the option tearoff=0 when  
creating the menus, indices will start ot 0.

But Marc's answer still applies: it's a lot of work for something that  
will usually be configured once. So requiring to restart the tool when the  
UI language changes should be acceptable.

 Thanks,
 Phil

HTH
-- 
python -c print ''.join([chr(154 - ord(c)) for c in  
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to change menu text with Tkinter?

2006-09-27 Thread Phil Schmidt

Eric Brunel wrote:



 But Marc's answer still applies: it's a lot of work for something that
 will usually be configured once. So requiring to restart the tool when the
 UI language changes should be acceptable.


Thanks for the example, that helps.

I agree with you and Marc regarding the language configuration method.
The requirements aren't mine however - my customer wants the language
selectable at runtime, so I'm kind of stuck with that.

Phil

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


Re: How to change font direction?

2006-09-18 Thread Frederic Rentsch
theju wrote:
 Well here are some self explanatory functions that I've written for
 displaying the text vertically and from right to left. As for rotation
 gimme some more time and i'll come back to you. Also I don't guarantee
 that this is the best method(cos I myself am a newbie), but I can
 guarantee you that it works.
 Here it goes...

 im=Image.open(imgfile.jpg)
 draw=ImageDraw.Draw(im)

 def verdraw(width,height,spacing,a=Defaulttext):
   for i in range(0,len(a)):
   draw.text((width,height),a[i],(options))
   height+=spacing

 def right2leftdraw(width,height,spacing,a=Defaulttext):
   for i in range(0,len(a)):
   draw.text((width,height),a[len(a)-i-1],(options))
   width += spacing

 options is a 3 field length tuple is mentioned in the PIL-Handbook.
 Hope you find it useful
 -Theju

 Daniel Mark wrote:
   
 Hello all:

 I am using PIL to draw some graphics and I need to draw some texts
 in vertical direction rather than the default left-to-right horizontal
 direction.

 Is there anyway I could do that?


 Thank you
 -Daniel
 

   

I have done a circular 24-hour dial with the numbers arranged to read 
upright as seen from the center. I remember writing each number into a 
frame, rotating the frame and pasting it into the dial image at the 
right place. I also remember using a transparency mask, so the white 
background of the little frame didn't cover up what it happened to 
overlap (minute marks). The numbers were black on white, so the frame 
was monochrome and could be used as its own transparency mask.
  (This could well be an needlessly complicated approach. When 
confronting a problem, I tend to weigh the estimated time of  hacking 
against the estimated time of shopping and reading recipes and often 
decide for hacking as the faster alternative.)

Regards

Frederic

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


Re: How to change font direction?

2006-09-15 Thread theju
Well here are some self explanatory functions that I've written for
displaying the text vertically and from right to left. As for rotation
gimme some more time and i'll come back to you. Also I don't guarantee
that this is the best method(cos I myself am a newbie), but I can
guarantee you that it works.
Here it goes...

im=Image.open(imgfile.jpg)
draw=ImageDraw.Draw(im)

def verdraw(width,height,spacing,a=Defaulttext):
for i in range(0,len(a)):
draw.text((width,height),a[i],(options))
height+=spacing

def right2leftdraw(width,height,spacing,a=Defaulttext):
for i in range(0,len(a)):
draw.text((width,height),a[len(a)-i-1],(options))
width += spacing

options is a 3 field length tuple is mentioned in the PIL-Handbook.
Hope you find it useful
-Theju

Daniel Mark wrote:
 Hello all:

 I am using PIL to draw some graphics and I need to draw some texts
 in vertical direction rather than the default left-to-right horizontal
 direction.
 
 Is there anyway I could do that?
 
 
 Thank you
 -Daniel

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


Re: How to change the path for python binary?

2006-08-10 Thread Amit Khemka
On 8/10/06, Nico Grubert [EMAIL PROTECTED] wrote:
 Hi there,

 I have installed Python 2.3.5 on Suse Linux 10.
 If I enter python in the shell, the Python 2.3.5 interpreter is called.

 After I installed Python 2.4.3. the Python 2.4.3 interpreter is called
 which is the default behaviour I guess.

 which python brings me /usr/local/bin/python which calls the Python
 2.4.3 interpreter.

 My question now is:
 What do I have to do in order to get the Python 2.3.5 interpreter each
 time I enter python in the shell?

There could be various ways of doing so, like:

Option 1: open your shell preference file ( ex. for bash ~/.bashrc)
and add an alias: alias
python=/path/to/python/version/of/your/choise

Option 2: make /usr/local/bin/python a symlink to python2.3.5

cheers,
amit.

-- 

Amit Khemka -- onyomo.com
Home Page: www.cse.iitd.ernet.in/~csd00377
Endless the world's turn, endless the sun's Spinning, Endless the quest;
I turn again, back to my own beginning, And here, find rest.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to change the path for python binary?

2006-08-10 Thread Alex Martelli
Nico Grubert [EMAIL PROTECTED] wrote:

 Hi there,
 
 I have installed Python 2.3.5 on Suse Linux 10.
 If I enter python in the shell, the Python 2.3.5 interpreter is called.
 
 After I installed Python 2.4.3. the Python 2.4.3 interpreter is called
 which is the default behaviour I guess.
 
 which python brings me /usr/local/bin/python which calls the Python
 2.4.3 interpreter.
 
 My question now is:
 What do I have to do in order to get the Python 2.3.5 interpreter each
 time I enter python in the shell?

One sufficient idea might be to put in your .bashrc file (or the like) a
statement such as

alias python=python2.3


Alex
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to change sys.path?

2006-05-25 Thread per9000
also se topic named
'problem(s) with import from parent dir: from ../brave.py import
sir_robin '

I use this every day now:
sys.path.append(../../py_scripts)

best wishes,
Per

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


RE: how to change sys.path?

2006-05-25 Thread Michael Yanowitz
  Is there something like a .pythoninitrc which can run whenever we start
Python
that can load a file with many sys.path.append(), etc?
  If not is there some way to modify the Python shell constructor and
destructor?

Thanks in advance:
Michael yanowitz

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Behalf
Of per9000
Sent: Thursday, May 25, 2006 4:07 AM
To: python-list@python.org
Subject: Re: how to change sys.path?


also se topic named
'problem(s) with import from parent dir: from ../brave.py import
sir_robin '

I use this every day now:
sys.path.append(../../py_scripts)

best wishes,
Per


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


Re: how to change sys.path?

2006-05-25 Thread Ziga Seilnacht
Michael Yanowitz wrote:
 Is there something like a .pythoninitrc which can run whenever we start
 Python
 that can load a file with many sys.path.append(), etc?
   If not is there some way to modify the Python shell constructor and
 destructor?

 Thanks in advance:
 Michael yanowitz

Yes, there is the user module:
http://docs.python.org/lib/module-user.html
which you have to explicitly import and which will look for
.pythonrc.py file in user's home directory and execute it.

The other option is a sitecustomize module, which should
be put somewhere on the initial search path. It will be
imported automatically during the interpreter initialization.
See:
http://docs.python.org/lib/module-site.html
for details.

Ziga

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


Re: how to change sys.path?

2006-05-25 Thread John Salerno
Dennis Lee Bieber wrote:
 On Wed, 24 May 2006 17:24:08 GMT, John Salerno
 [EMAIL PROTECTED] declaimed the following in comp.lang.python:
 
 Dennis Lee Bieber wrote:

 I may have gotten slightly confused
 That's my job. :)
 
   Okay:
 
   You have gotten me slightly confused G

Your job is to pay no attention to me and my blathering. :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to change sys.path?

2006-05-24 Thread Steve Holden
Ju Hui wrote:
 yes, we can change PYTHONPATH to add some path to sys.path value, but
 how to remove item from sys.path?
 
That would be

del sys.path[3]

for example. Of course you may need to search sys.path to determine the 
exact element you need to delete, but sys.path is just like any other 
list in Python and can (as many people have already said in this thread) 
be manipulated just like al the others.

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd  http://www.holdenweb.com
Love me, love my blog  http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

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


Re: how to change sys.path?

2006-05-24 Thread John Salerno
Dennis Lee Bieber wrote:
 On Tue, 23 May 2006 20:21:10 GMT, John Salerno
 [EMAIL PROTECTED] declaimed the following in comp.lang.python:
 
 I meant actually adding the PYTHONPATH variable to the environment 
 variables list.
 
   You're looking at editing the Windows registry for that...

I just right-clicked on My Computer -- Properties -- Advanced -- 
Environment Variables, and added a new one called PYTHONPATH. I don't 
know if that edits the registry, but you don't *manually* have to edit 
the registry if you do it that way...unless of course you aren't 
supposed to be doing it that way! But it worked anyway. :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to change sys.path?

2006-05-24 Thread Andrew Robert
Dennis Lee Bieber wrote:
 On Wed, 24 May 2006 14:45:55 GMT, John Salerno
 [EMAIL PROTECTED] declaimed the following in comp.lang.python:
 
 I just right-clicked on My Computer -- Properties -- Advanced -- 
 Environment Variables, and added a new one called PYTHONPATH. I don't 
 know if that edits the registry, but you don't *manually* have to edit 
 the registry if you do it that way...unless of course you aren't 
 supposed to be doing it that way! But it worked anyway. :)
 
   I may have gotten slightly confused -- I had an impression that, at
 least one poster in the thread, wanted to do this from within a Python
 program. That does go into registry modifications.
 
   For example, look at the entries under:
 
 (system environment, I believe)
 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session
 Manager\Environment
 
 (user specific environment)
 HKEY_CURRENT_USER\Environment
 
   Though in the case of PYTHONPATH, the core value seems to be in (for
 my install)
 
 HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.3\PythonPath
 
 which also has subkeys for Pythonwin, win32, and win32com
 

Instead of messing with the registry, wouldn't it be easier to just add
a line similar to this within your code?

sys.path.append(r'\\mynetwork\share')

I use something similar so that all my scripts can locate the same
home-grown modules no matter where they are run from.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to change sys.path?

2006-05-24 Thread John Salerno
Dennis Lee Bieber wrote:

   I may have gotten slightly confused

That's my job. :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to change sys.path?

2006-05-24 Thread Ju Hui
yes, we can add path to PYTHONPATH,but how to remove some items?
my sys.path:
 import sys
 for x in sys.path:
... print x
...

D:\usr\local\lib\site-packages\setuptools-0.6a11-py2.4.egg
D:\usr\local\lib\site-packages\clientcookie-1.3.0-py2.4.egg
c:\temp
C:\WINDOWS\system32\python24.zip
C:\Documents and Settings\phpbird
D:\usr\local\DLLs
D:\usr\local\lib
D:\usr\local\lib\plat-win
D:\usr\local\lib\lib-tk
D:\usr\local
D:\usr\local\lib\site-packages


the value of
HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.4\PythonPath
D:\usr\local\Lib;D:\usr\local\DLLs;D:\usr\local\Lib\lib-tk;

the content are difference, how to remove
C:\WINDOWS\system32\python24.zip
or
D:\usr\local\lib\site-packages\clientcookie-1.3.0-py2.4.egg
?

thanks.

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


Re: how to change sys.path?

2006-05-24 Thread [EMAIL PROTECTED]
sys.append(write path of the module u r importing)

Bye.

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


Re: how to change sys.path?

2006-05-24 Thread [EMAIL PROTECTED]
sys.path.append(path containing module to be imported)

eg. sys.path.append(/home/webdoc)

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


Re: how to change sys.path?

2006-05-23 Thread bruno at modulix
Ju Hui wrote:
 is python search module by paths in sys.path?

sys.path is the list of path where the Python interpreter will search
modules, yes.

 how to change it manuallly?

manually ?-)

You mean dynamically, by code ? If yes, it's just a list. You can
modify it like you'd do for any other list.

Else, you may want to look at the PYTHON_PATH environnement variable.

-- 
bruno desthuilliers
python -c print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to change sys.path?

2006-05-23 Thread Ju Hui
yes, I mean I want change the sys.path value and save it for next
using.
I can change the value of sys.path, but I can't save it permanently.
There is no python_path environment on my pc, what the relationship
between it and the sys.path?

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


Re: how to change sys.path?

2006-05-23 Thread John Salerno
Ju Hui wrote:
 yes, I mean I want change the sys.path value and save it for next
 using.
 I can change the value of sys.path, but I can't save it permanently.
 There is no python_path environment on my pc, what the relationship
 between it and the sys.path?
 

In Windows, at least, you can create the PYTHONPATH variable and assign 
to it the paths of the directories you want Python to check for when 
running a script.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to change sys.path?

2006-05-23 Thread Jarek Zgoda
John Salerno napisał(a):

 yes, I mean I want change the sys.path value and save it for next
 using.
 I can change the value of sys.path, but I can't save it permanently.
 There is no python_path environment on my pc, what the relationship
 between it and the sys.path?
 
 In Windows, at least, you can create the PYTHONPATH variable and assign
 to it the paths of the directories you want Python to check for when
 running a script.

Setting Windows envvar by changing os.environ dict will work only for
processes running in the same environment (i.e. in the same shell session).

-- 
Jarek Zgoda
http://jpa.berlios.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   >