log file.

2016-06-14 Thread owenpaul . po
 I have a programme to pump out water from a sump and would like to log to a 
readable file when the pump operates. what is the easiest way to acheive this 
with python 3.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: log file.

2016-06-14 Thread Jussi Piitulainen
owenpaul...@gmail.com writes:

> I have a programme to pump out water from a sump and would like to log
> to a readable file when the pump operates. what is the easiest way to
> acheive this with python 3.

Depending on any number of details, the easiest may be to just print.
Direct stdout and stderr to files when you run the program.
-- 
https://mail.python.org/mailman/listinfo/python-list


mod_python compilation error in VS 2008 for py2.7.1

2016-06-14 Thread asimkon
I would like to ask you a technical question regarding python module
compilation for python 2.7.1.

I want to compile mod_python
library
for Apache 2.2 and py2.7  on Win32 in
order to use it for  psp - py scripts that i have written. I tried to
compile it using VS 2008 (VC++) and unfortunately i get an error on
pyconfig.h (Py2.7/include) error C2632: int followed by int  is illegal.

This problem occurs when i try to run the bat file that exists on
mod_python/dist folder. Any idea or suggestion what should i do in order to
run it on Win 7 Pro (win 32) environment and produce the final apache
executable module (.so).

 I have posted the same question here
,
but unfortunately i had had no luck!


Additionally i give you the compilation instructions that i follow (used
also MinGW-w64 and get the same error) in order to produce the final output!

Compiling

Open a command prompt with VS2008 support. The easiest way to do this is to
use "Start | All Programs | Microsoft Visual Studio 2008 | Visual Studio
Tools | Visual Studio 2008 Command Prompt". (This puts the VS2008 binaries
in the path and sets up the lib/include environmental variables for the
Platform SDK.)

1.cd to the mod_python\dist folder.

2.Tell mod_python where Apache is: set APACHESRC=C:\Apache

3. Run build_installer.bat.

If it succeeds, an installer.exe will be created in a subfolder. Run that
install the module.


Kind  Regards

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


Re: for / while else doesn't make sense

2016-06-14 Thread alister
On Tue, 14 Jun 2016 12:43:35 +1000, Steven D'Aprano wrote:
> 
> On this list, I daresay somebody will insist that if their computer is
> on one of Jupiter's moons it will keep running fine and therefore I'm
> wrong.
> 

Anyone on a moon of Jupiter would not be able to get internet access
(TCP/IP time-outs mean that even Mars is outside of internet range. The 
Moon at 1.3 light seconds might manage it ) so they could not be posting 
here ;-)



-- 
Drink Canada Dry!  You might not succeed, but it *__is* fun trying.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: mod_python compilation error in VS 2008 for py2.7.1

2016-06-14 Thread Pavel S
Have you considered to use rather WSGI-based solution? (for Apache Httpd is 
mod_wsgi). Mod_python is totally obsolete.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: for / while else doesn't make sense

2016-06-14 Thread Ian Kelly
On Tue, Jun 14, 2016 at 5:39 AM, alister  wrote:
> On Tue, 14 Jun 2016 12:43:35 +1000, Steven D'Aprano wrote:
>>
>> On this list, I daresay somebody will insist that if their computer is
>> on one of Jupiter's moons it will keep running fine and therefore I'm
>> wrong.
>>
>
> Anyone on a moon of Jupiter would not be able to get internet access
> (TCP/IP time-outs mean that even Mars is outside of internet range. The
> Moon at 1.3 light seconds might manage it ) so they could not be posting
> here ;-)

Well, TCP might be problematic, but radio transmission from Jupiter
would still be faster than RFC 1149, which in its only field test took
an average of 5200 seconds to send round-trip pings over a distance of
3 miles.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: for / while else doesn't make sense

2016-06-14 Thread Chris Angelico
On Tue, Jun 14, 2016 at 11:51 PM, Ian Kelly  wrote:
> On Tue, Jun 14, 2016 at 5:39 AM, alister  wrote:
>> On Tue, 14 Jun 2016 12:43:35 +1000, Steven D'Aprano wrote:
>>>
>>> On this list, I daresay somebody will insist that if their computer is
>>> on one of Jupiter's moons it will keep running fine and therefore I'm
>>> wrong.
>>>
>>
>> Anyone on a moon of Jupiter would not be able to get internet access
>> (TCP/IP time-outs mean that even Mars is outside of internet range. The
>> Moon at 1.3 light seconds might manage it ) so they could not be posting
>> here ;-)
>
> Well, TCP might be problematic, but radio transmission from Jupiter
> would still be faster than RFC 1149, which in its only field test took
> an average of 5200 seconds to send round-trip pings over a distance of
> 3 miles.

And I'm sure someone could figure out a "netnews-over-UDP" system.
After all, most people just post without thinking about responses
anyway, which is exactly what UDP is great at...

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


Re: for / while else doesn't make sense

2016-06-14 Thread Rustom Mody
On Tuesday, June 14, 2016 at 8:13:53 AM UTC+5:30, Steven D'Aprano wrote:
> No. The sun exploding was me gently mocking you for your comment disputing
> the "unconditional" part. Yes, you are technically right that technically
> the "else" block will only run if no "break" is reached, and no "return" is
> reached, no exception is raised, also that os._exit or os.abort aren't
> called, the CPU doesn't catch fire, and the world isn't destroyed.
> 
> If we try to enumerate all the things which could prevent the "else" block
> from running, we'll be here for decades. But, and this is the point that
> everyone seems to have missed, * every single one of those things* is
> completely independent of the for...else statement.
> 
> *Including* the presence or absence of a "break".

This is real wild:  A break that is inside a for is independent of the for?!?!

Thats about as meaningful a statement as saying that the algebraic expression
"x² + 1" has a value independent of "x"

However see below

> 
> If you want to understand how Python statements work, you should understand
> them in isolation (as much as possible), which then allows you to
> extrapolate their behaviour in combination with other statements. Most
> lines of Python code are understandable in isolation, or at least close to
> isolation. You can get *very close* to a full understanding of Python by
> just reading one line at a time (with perhaps a bit of short term memory to
> remember if you are compiling a function, building a class, etc).
> 
> E.g. you don't need to understand for loops to understand if...else.
> 
> And vice versa: for...else has a well-defined meaning and operates in a
> simple fashion in isolation of other language features. Its not compulsory
> to put a "return" statement inside your for loop. Nor is it compulsory to
> put a "raise" inside it. And "break" is not compulsory either.

This *desire* for what you call isolation is a standard tenet of
semantics and is right

It is called compositionality

Inn programming: 
https://en.wikipedia.org/wiki/Denotational_semantics#Compositionality

More general linguistics: 
https://en.wikipedia.org/wiki/Principle_of_compositionality

However gotos break compositionality unless one introduces heavy artillery like
continuations

And break is a euphemism for goto
-- 
https://mail.python.org/mailman/listinfo/python-list


Multiple files reading

2016-06-14 Thread Joaquin Alzola
Hi Guys

I am doing  program that reads into a directory for the files that were created 
the last 5 mins. (working)

Inside those files there are 242 fields in each line separated by | (pipe). 
Each file has about 5k records and there are about 5 files per 5 mins.

I will look for field 29 and 200 (Country, Diameter Error code). (split)

I have 6 different countries (which I differentiate by field 29 which is the 
CountryCode).

The thing is that I make it work but it goes slow. On the parent class I read 
the files all over for each Country 150k lines read (because I read 25k for 
each country which makes a total of 150k lines read).
So the code even working is inefficient.

I created classes such as:

Read Files (Parent) - Country -- Service

The dictionary that I am using in the classes: 
{'Country':'Empty','Service':'Empty','TimeStamp':'Empty','Ocg':'see3','DiameterCodes':{'2001':0,'4010':0,'4012':0,'4998':0,'4999':0,'5007':0,'5012':0}}

Wanted help from your side on how to focus this just because I want to read the 
files once (not 6 times) and then use classes to get back the dictionary value 
...

I need just advice on steps to follow...

Thanks

J
This email is confidential and may be subject to privilege. If you are not the 
intended recipient, please do not copy or disclose its content but contact the 
sender immediately upon receipt.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: log file.

2016-06-14 Thread Paul Owen
I am very inexperienced at programming.!

is there a lot of code needed to use those modules.

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


Re: log file.

2016-06-14 Thread John Gordon
In <05d2df77-8cd0-467b-8ab3-54bf730d8...@googlegroups.com> 
owenpaul...@gmail.com writes:

> I have a programme to pump out water from a sump and would like to
> log to a readable file when the pump operates. what is the easiest
> way to acheive this with python 3.

Are you asking for help with logging, or communicating with the pump?

-- 
John Gordon   A is for Amy, who fell down the stairs
gor...@panix.com  B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

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


Re: Multiple files reading

2016-06-14 Thread MRAB

On 2016-06-14 17:53, Joaquin Alzola wrote:

Hi Guys

I am doing  program that reads into a directory for the files that were created 
the last 5 mins. (working)

Inside those files there are 242 fields in each line separated by | (pipe). 
Each file has about 5k records and there are about 5 files per 5 mins.

I will look for field 29 and 200 (Country, Diameter Error code). (split)

I have 6 different countries (which I differentiate by field 29 which is the 
CountryCode).

The thing is that I make it work but it goes slow. On the parent class I read 
the files all over for each Country 150k lines read (because I read 25k for 
each country which makes a total of 150k lines read).
So the code even working is inefficient.

I created classes such as:

Read Files (Parent) - Country -- Service

The dictionary that I am using in the classes: 
{'Country':'Empty','Service':'Empty','TimeStamp':'Empty','Ocg':'see3','DiameterCodes':{'2001':0,'4010':0,'4012':0,'4998':0,'4999':0,'5007':0,'5012':0}}

Wanted help from your side on how to focus this just because I want to read the 
files once (not 6 times) and then use classes to get back the dictionary value 
...

I need just advice on steps to follow...

Use a dict (or defaultdict) where the key is the country and the value 
is info (class?) related to that country.


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


Re: log file.

2016-06-14 Thread Paul Owen
logging please.

my pump programme works but I want to log the time etc. when the pump runs and 
stops.

I am trying to improve the programme 

I am a novice!
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: log file.

2016-06-14 Thread Joaquin Alzola
>logging please.

Check this modules:
import logging
import logging.handlers
This email is confidential and may be subject to privilege. If you are not the 
intended recipient, please do not copy or disclose its content but contact the 
sender immediately upon receipt.
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: log file.

2016-06-14 Thread Paul Owen
thanks I will look at them.
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Multiple files reading

2016-06-14 Thread Joaquin Alzola
>> The dictionary that I am using in the classes:
>> {'Country':'Empty','Service':'Empty','TimeStamp':'Empty','Ocg':'see3',
>> 'DiameterCodes':{'2001':0,'4010':0,'4012':0,'4998':0,'4999':0,'5007':0
>> ,'5012':0}}
>>
>> Wanted help from your side on how to focus this just because I want to read 
>> the files once (not 6 times) and then use classes to get back the dictionary 
>> value ...
>>
>> I need just advice on steps to follow...
>>
>Use a dict (or defaultdict) where the key is the country and the value is info 
>(class?) related to that country.

Putting a dict for each country such as:
self.dictionaryNL = 
{'Country':'Empty','Service':'Empty','TimeStamp':'Empty','Ocg':'see3','DiameterCodes':{'2001':0,'4010':0,'4012':0,'4998':0,'4999':0,'5007':0,'5012':0}}
 self.dictionarySP = 
{'Country':'Empty','Service':'Empty','TimeStamp':'Empty','Ocg':'see3','DiameterCodes':{'2001':0,'4010':0,'4012':0,'4998':0,'4999':0,'5007':0,'5012':0}}
 self.dictionaryUK = 
{'Country':'Empty','Service':'Empty','TimeStamp':'Empty','Ocg':'see3','DiameterCodes':{'2001':0,'4010':0,'4012':0,'4998':0,'4999':0,'5007':0,'5012':0}}
 self.dictionaryFR = 
{'Country':'Empty','Service':'Empty','TimeStamp':'Empty','Ocg':'see3','DiameterCodes':{'2001':0,'4010':0,'4012':0,'4998':0,'4999':0,'5007':0,'5012':0}}
 self.dictionaryDK = 
{'Country':'Empty','Service':'Empty','TimeStamp':'Empty','Ocg':'see3','DiameterCodes':{'2001':0,'4010':0,'4012':0,'4998':0,'4999':0,'5007':0,'5012':0}}
 self.dictionaryGR = 
{'Country':'Empty','Service':'Empty','TimeStamp':'Empty','Ocg':'see3','DiameterCodes':{'2001':0,'4010':0,'4012':0,'4998':0,'4999':0,'5007':0,'5012':0}}

makes the trick but I have to repeat 6 time all the time. Now with that config 
I read only once the files saving 125k read lines.

But as always I think it can be done better (just started to learn python about 
8 month ago). Thanks for the tips.

BR

Joaquin

This email is confidential and may be subject to privilege. If you are not the 
intended recipient, please do not copy or disclose its content but contact the 
sender immediately upon receipt.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: log file.

2016-06-14 Thread Joel Goldstick
On Tue, Jun 14, 2016 at 4:14 PM, Paul Owen  wrote:
> thanks I will look at them.
> --
> https://mail.python.org/mailman/listinfo/python-list


If your program is called 'my_program.py'  you can write print
statements (or print functions in python 3) and when you run your
program, those prints will show on your screen.  If you want to save
them to a log file do this:

python my_program.py >> my_log_file.log

When your program ends, open up my_log_file.log to see what happened.
The two > characters will write to the file if it is new, or append to
the file if it exists


-- 
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Multiple files reading

2016-06-14 Thread MRAB

On 2016-06-14 21:06, Joaquin Alzola wrote:
> >> The dictionary that I am using in the classes:
> >> {'Country':'Empty','Service':'Empty','TimeStamp':'Empty','Ocg':'see3',
> >> 'DiameterCodes':{'2001':0,'4010':0,'4012':0,'4998':0,'4999':0,'5007':0
> >> ,'5012':0}}
> >>
> >> Wanted help from your side on how to focus this just because I 
want to read the files once (not 6 times) and then use classes to get 
back the dictionary value ...

> >>
> >> I need just advice on steps to follow...
> >>
> >Use a dict (or defaultdict) where the key is the country and the 
value is info (class?) related to that country.

>
> Putting a dict for each country such as:
> self.dictionaryNL = 
{'Country':'Empty','Service':'Empty','TimeStamp':'Empty','Ocg':'see3','DiameterCodes':{'2001':0,'4010':0,'4012':0,'4998':0,'4999':0,'5007':0,'5012':0}}
>  self.dictionarySP = 
{'Country':'Empty','Service':'Empty','TimeStamp':'Empty','Ocg':'see3','DiameterCodes':{'2001':0,'4010':0,'4012':0,'4998':0,'4999':0,'5007':0,'5012':0}}
>  self.dictionaryUK = 
{'Country':'Empty','Service':'Empty','TimeStamp':'Empty','Ocg':'see3','DiameterCodes':{'2001':0,'4010':0,'4012':0,'4998':0,'4999':0,'5007':0,'5012':0}}
>  self.dictionaryFR = 
{'Country':'Empty','Service':'Empty','TimeStamp':'Empty','Ocg':'see3','DiameterCodes':{'2001':0,'4010':0,'4012':0,'4998':0,'4999':0,'5007':0,'5012':0}}
>  self.dictionaryDK = 
{'Country':'Empty','Service':'Empty','TimeStamp':'Empty','Ocg':'see3','DiameterCodes':{'2001':0,'4010':0,'4012':0,'4998':0,'4999':0,'5007':0,'5012':0}}
>  self.dictionaryGR = 
{'Country':'Empty','Service':'Empty','TimeStamp':'Empty','Ocg':'see3','DiameterCodes':{'2001':0,'4010':0,'4012':0,'4998':0,'4999':0,'5007':0,'5012':0}}

>
> makes the trick but I have to repeat 6 time all the time. Now with 
that config I read only once the files saving 125k read lines.

>
> But as always I think it can be done better (just started to learn 
python about 8 month ago). Thanks for the tips.

>
I think that what you're doing currently is scanning the files multiple 
times, once for each country, looking in the appropriate field for the 
country to see whether you should read or skip.


What I meant was that you would have a dict of dicts, where the key was 
the country:


self.dictionary = {}

for country in ('NL', 'SP', 'UK', 'FR', 'DK', 'GR'):
self.dictionary[country] = 
{'Country':'Empty','Service':'Empty','TimeStamp':'Empty','Ocg':'see3','DiameterCodes':{'2001':0,'4010':0,'4012':0,'4998':0,'4999':0,'5007':0,'5012':0}}


You could then scan the files _once_, looking in the appropriate field 
for the country, and then updating the appropriate dictionary.


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


RE: log file.

2016-06-14 Thread Joaquin Alzola
>thanks I will look at them.

As an example for your guide:

##log_handler###
import sys
import logging
import logging.handlers
from configfile_read import *

def LogHandler(logger):
CONFIG_FILE = ('./conf/' + 'config.ini')
config = configfile(CONFIG_FILE)

FORMAT = '%(asctime)-15s %(name)s %(levelname)s-%(levelno)s %(message)s'
LOG_LEVEL = config.get("logging","Logging_Level")
LOG_FILE = config.get("logging","Logging_File")
LOG_DIRECTORY = config.get("logging","Logging_Directory")
LOG_BYTES = config.get("logging","Logging_bytes")
LOG_COUNT = config.get("logging","Logging_count")

LEVEL = logging.getLevelName(LOG_LEVEL)
logger.setLevel(LEVEL)
handler = logging.handlers.RotatingFileHandler(LOG_DIRECTORY + 
LOG_FILE,maxBytes=int(LOG_BYTES),backupCount=int(LOG_COUNT))
handler.setFormatter(logging.Formatter(FORMAT))
return handler


On each part of your code start a logger:
logger = logging.getLogger('__main__.' + __name__)

And in the main part:
from log_handler import *
#Logging Config Starts
logger = logging.getLogger(__name__)
logger.addHandler(LogHandler(logger))
#Logging Config Ends

Config_parser where log parameters are taken
import configparser
import os, sys
import logging
import logging.handlers
from log_handler import *

logger = logging.getLogger('__main__.' + __name__)

class configfile:

def __init__(self,CONFIG_FILE):
self.CONFIG_FILE = CONFIG_FILE
if not os.path.isfile(self.CONFIG_FILE):
logger.critical('File NOT found: %s' % CONFIG_FILE + '. 
This file is needed for the system to work')
sys.exit()

def get(self,general,parameter):
config = configparser.ConfigParser()
config.read(self.CONFIG_FILE)
try:
result = config.get(general,parameter)
except configparser.NoSectionError as e:
logger.critical('File %s is there but need information 
inside: %s'%(self.CONFIG_FILE,e))
sys.exit()
logger.debug('Parameters pass to configparser-->' + general + 
':' + parameter + ' with result-->' + result)
return result


testbedocg:/usr/local/statistics_server/conf # more config.ini
[config]
Host=172.1.1.1
Port=12346

[logging]
#Possbile Values: CRITICAL ERROR WARNING INFO DEBUG NOTSET
Logging_Level=DEBUG
Logging_File=server_log.log
Logging_Directory=./logs/
Logging_bytes=2097152
Logging_count=5

This email is confidential and may be subject to privilege. If you are not the 
intended recipient, please do not copy or disclose its content but contact the 
sender immediately upon receipt.
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: log file.

2016-06-14 Thread Paul Owen

Gmail

Google+

Calendar

Web

more

Inbox

pump programme

Paul Owen

to me
1 hour ago

Details

from gpiozero import LED,Button

from signal import pause

print ("Pump Programme Running")

led = LED(17)

low = Button (2)

high = Button (3)

high.when_pressed = led.on 

low.when_released = led.off

#as you can see I need a simple way to log when led(pump) runs

hope seeing this helps.

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


RE: Multiple files reading

2016-06-14 Thread Joaquin Alzola
> What I meant was that you would have a dict of dicts, where the key was the 
> country:

Thanks MRAB I could not see that solution. That save me a lot of lines of code.
Certainly my previous solution also manage to do that but yours is more 
clean-code wise.
This email is confidential and may be subject to privilege. If you are not the 
intended recipient, please do not copy or disclose its content but contact the 
sender immediately upon receipt.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: for / while else doesn't make sense

2016-06-14 Thread Rustom Mody
On Wednesday, June 15, 2016 at 4:58:05 AM UTC+5:30, Lawrence D’Oliveiro wrote:
> On Wednesday, June 15, 2016 at 3:34:14 AM UTC+12, Rustom Mody wrote:
> 
> > And break is a euphemism for goto
> 
> Is this the old 
> “structured-programming-is-mathematically-equivalent-to-gotos” red herring 
> again?

Are you familiar with Duff's device? 

https://en.wikipedia.org/wiki/Duff%27s_device
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: for / while else doesn't make sense

2016-06-14 Thread Steven D'Aprano
On Wed, 15 Jun 2016 01:33 am, Rustom Mody wrote:

> On Tuesday, June 14, 2016 at 8:13:53 AM UTC+5:30, Steven D'Aprano wrote:
>> No. The sun exploding was me gently mocking you for your comment
>> disputing the "unconditional" part. Yes, you are technically right that
>> technically the "else" block will only run if no "break" is reached, and
>> no "return" is reached, no exception is raised, also that os._exit or
>> os.abort aren't called, the CPU doesn't catch fire, and the world isn't
>> destroyed.
>> 
>> If we try to enumerate all the things which could prevent the "else"
>> block from running, we'll be here for decades. But, and this is the point
>> that everyone seems to have missed, * every single one of those things*
>> is completely independent of the for...else statement.
>> 
>> *Including* the presence or absence of a "break".
> 
> This is real wild:  A break that is inside a for is independent of the
> for?!?!

If that's what I said, you would be right to question me. But that's not
what I said.

It is legal syntax to have for...else without a break, or a break inside a
for block with no else. And, if you really want to nitpick, you can even
have a break statement without a for. (Just stick it inside a while loop
instead.)

I know that's it's great fun to pick at nits without making a good faith
effort to communicate, but honestly Rustom, your following comments do
suggest that you understood what I was saying.


[...]
> This *desire* for what you call isolation is a standard tenet of
> semantics and is right
> 
> It is called compositionality
> 
> Inn programming:
> https://en.wikipedia.org/wiki/Denotational_semantics#Compositionality
> 
> More general linguistics:
> https://en.wikipedia.org/wiki/Principle_of_compositionality

Thanks for that.


> However gotos break compositionality unless one introduces heavy artillery
> like continuations
> 
> And break is a euphemism for goto

Sort of. A break is a jump, and a goto is a jump, but apart from that,
they're not really the same thing. A goto can jump (almost) anywhere.
Depending on the language, they can jump into the middle of functions, or
into the middle of loops. That's what makes them powerful enough to break
compositionality. But break can only jump to a single place: to the
statement that follows the for...else compound statement. It's more like a
return than a goto.

You can reason about for...else without the break, then reason about what
the break does. This isn't hard, and its what people do even in the common
use-case:

for x in seq:
process(x)
if condition:
break
else:
fnord()
spam()

"If the condition is never true, then we loop through seq, calling
process(x) each time, then call fnord(), then call spam(). If the condition
becomes true at some point in the loop, we stop looping, and go straight to
calling spam()."

We can reason about the condition is true case separately from the condition
is false case. And we can do so without imagining that there is an
invisible "did_not_break" flag, or needing a second mental model to
understand this ever-so-slightly more complex example:

for x in seq:
process(x)
if condition:
break
else:
foo()
else:
fnord()
spam()



-- 
Steven

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


Re: log file.

2016-06-14 Thread Steven D'Aprano
On Wed, 15 Jun 2016 05:54 am, Paul Owen wrote:

> logging please.
> 
> my pump programme works but I want to log the time etc. when the pump runs
> and stops.
> 
> I am trying to improve the programme
> 
> I am a novice!

As a novice, start with the simplest thing. Whenever the pump runs or stops,
just call print:


start(pump)
print("Starting")
...
stop(pump)
print("Stopping")


To log to a file instead, change the program to:


import logging
logging.basicConfig(filename='example.log',level=logging.DEBUG)
start(pump)
logging.info("Starting")
...
stop(pump)
logging.info("Stopping")


More information in the logging tutorial:

https://docs.python.org/2/howto/logging.html
https://docs.python.org/3/howto/logging.html



-- 
Steven

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


python regex: variable length of positive lookbehind assertion

2016-06-14 Thread Yubin Ruan
Hi everyone, 
I am struggling writing a right regex that match what I want:

Problem Description:

Given a string like this:

>>>string = "false_head aaa bbb false_tail \
 true_head some_text_here ccc ddd eee 
true_tail"

I want to match the all the text surrounded by those " ",
but only if those " " locate **in some distance** behind "true_head". 
That is, I expect to result to be like this:

>>>import re
>>>result = re.findall("the_regex",string)
>>>print result
["ccc","ddd","eee"]

How can I write a regex to match that?
I have try to use the **positive lookbehind assertion** in python regex,
but it does not allowed variable length of lookbehind.

Thanks in advance,
Ruan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: for / while else doesn't make sense

2016-06-14 Thread Rustom Mody
On Wednesday, June 15, 2016 at 8:42:33 AM UTC+5:30, Steven D'Aprano wrote:
> On Wed, 15 Jun 2016 01:33 am, Rustom Mody wrote:
> 
> > On Tuesday, June 14, 2016 at 8:13:53 AM UTC+5:30, Steven D'Aprano wrote:
> >> No. The sun exploding was me gently mocking you for your comment
> >> disputing the "unconditional" part. Yes, you are technically right that
> >> technically the "else" block will only run if no "break" is reached, and
> >> no "return" is reached, no exception is raised, also that os._exit or
> >> os.abort aren't called, the CPU doesn't catch fire, and the world isn't
> >> destroyed.
> >> 
> >> If we try to enumerate all the things which could prevent the "else"
> >> block from running, we'll be here for decades. But, and this is the point
> >> that everyone seems to have missed, * every single one of those things*
> >> is completely independent of the for...else statement.
> >> 
> >> *Including* the presence or absence of a "break".
> > 
> > This is real wild:  A break that is inside a for is independent of the
> > for?!?!
> 
> If that's what I said, you would be right to question me. But that's not
> what I said.
> 
> It is legal syntax to have for...else without a break, or a break inside a
> for block with no else. And, if you really want to nitpick, you can even
> have a break statement without a for. (Just stick it inside a while loop
> instead.)
> 
> I know that's it's great fun to pick at nits without making a good faith
> effort to communicate, but honestly Rustom, your following comments do
> suggest that you understood what I was saying.
> 
> 
> [...]
> > This *desire* for what you call isolation is a standard tenet of
> > semantics and is right
> > 
> > It is called compositionality
> > 
> > Inn programming:
> > https://en.wikipedia.org/wiki/Denotational_semantics#Compositionality
> > 
> > More general linguistics:
> > https://en.wikipedia.org/wiki/Principle_of_compositionality
> 
> Thanks for that.
> 
> 
> > However gotos break compositionality unless one introduces heavy artillery
> > like continuations
> > 
> > And break is a euphemism for goto
> 
> Sort of. A break is a jump, and a goto is a jump, but apart from that,
> they're not really the same thing. A goto can jump (almost) anywhere.
> Depending on the language, they can jump into the middle of functions, or
> into the middle of loops. That's what makes them powerful enough to break
> compositionality. But break can only jump to a single place: to the
> statement that follows the for...else compound statement. It's more like a
> return than a goto.
> 
> You can reason about for...else without the break, then reason about what
> the break does. This isn't hard, and its what people do even in the common
> use-case:
> 
> for x in seq:
> process(x)
> if condition:
> break
> else:
> fnord()
> spam()

Yon need to take an example of the
if condition: break
nested inside some more ifs
with those other conditions giving validity to the condition
eg outer condition being say y != 0
Inner if being 
if x/y == 0 : break

Now you would see that your reasoning about the inner needs potentially the 
FULL CONTEXT  of the outer.
This need to carry large context is the essential property of non-compositional 
semantics.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: loading trees...

2016-06-14 Thread Lawrence D’Oliveiro
On Monday, June 13, 2016 at 6:56:07 AM UTC+12, Fillmore wrote:
> I then need to run a program that loads the whole forest in the form of a
> dict() where each item will point to a dynamically loaded tree.

Store it in JSON form?

I like language-neutral solutions.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python regex: variable length of positive lookbehind assertion

2016-06-14 Thread Yubin Ruan
On Wednesday, June 15, 2016 at 12:18:31 PM UTC+8, Lawrence D’Oliveiro wrote:
> On Wednesday, June 15, 2016 at 3:28:37 PM UTC+12, Yubin Ruan wrote:
> 
> > I want to match the all the text surrounded by those " ",
> 
> You are trying to use regex (type 3 grammar) to parse HTML (type 2 grammar) 
> ?
> 
> No can do 
> .


Yes. I think you are correct. Thanks.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python regex: variable length of positive lookbehind assertion

2016-06-14 Thread Jussi Piitulainen
Yubin Ruan writes:

> Hi everyone, 
> I am struggling writing a right regex that match what I want:
>
> Problem Description:
>
> Given a string like this:
>
> >>>string = "false_head aaa bbb false_tail \
>  true_head some_text_here ccc ddd eee 
> true_tail"
>
> I want to match the all the text surrounded by those " ", but
> only if those " " locate **in some distance** behind
> "true_head". That is, I expect to result to be like this:
>
> >>>import re
> >>>result = re.findall("the_regex",string)
> >>>print result
> ["ccc","ddd","eee"]
>
> How can I write a regex to match that?
> I have try to use the **positive lookbehind assertion** in python regex,
> but it does not allowed variable length of lookbehind.

Don't.

Don't even try to do it all in one regex. Keep your regexen simple and
match in two steps.

For example, capture all such elements together with your marker:

re.findall(r'true_head|[^<]+', string)
==>
['aaa', 'bbb',
 'true_head', 'ccc', 'ddd', 'eee']

Then filter the result in the obvious way (not involving any regex any
more, unless needed to recognize the true 'true_head' again). I've kept
the tags at this stage, so a possible 'true_head' won't look like
'true_head' yet.

Another way is to find 'true_head' first (if you can recognize it safely
before also recognizing the elements), and then capture the elements in
the latter half only.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: log file.

2016-06-14 Thread Paul Owen
Thank you Steven.
that is just what I need.

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