Re: # of Months between two dates

2018-04-05 Thread ankit . singh
On Sunday, August 8, 2010 at 11:46:51 PM UTC+5:30, MRAB wrote:
> Greg Lindstrom wrote:
> > I work for a company that processes claims for the health care industry 
> > (Novasys Health, recently purchased by Centene Corp).  My current 
> > assignment has me writing a routine to compute insurance premiums.  One 
> > of the requirements is to determine how many months a policy has been in 
> > effect.  The datetime module will give me the number of days but, with 
> > months having different lengths, that does not do me much good.  I've 
> > looked in the calendar library but didn't see anything there, either.
> > 
> > I've written a function to return the months between date1 and date2 but 
> > I'd like to know if anyone is aware of anything in the standard library 
> > to do the same?  For bonus points, does anyone know if postgres can do 
> > the same (we use a lot of date/time funcitons in postgres, already, but 
> > didn't see this problem addressed).
> > 
> [snip]
> A simple expression is:
> 
>  diff = (current_year - start_year) * 12 + (current_month - start_month)
> 
> According to this, if a policy started on 31 July 2010, then on 1 August
> 2010 it has been in effect for 1 month. Is this reasonable? It depends!
> 
> It's probably better to write the function yourself according to what
> makes sense in your use-case, and document its behaviour clearly.

Thanks for that answer
-- 
https://mail.python.org/mailman/listinfo/python-list


RegEx to match set of lines

2018-04-05 Thread Prahallad Achar
Hello,
I would like to match set of lines from the below data. this data comes
from one of the network

Part of data : [ If circuit type is OCHCC then I need Circuit name and
service ID from that group ]


Circuit ID: ad8a0165:25
*Circuit Name: T3A_100G_SD20*
Circuit Version: 0
Circuit Monitor: false
Circuit Size: OCH-TRAIL
*Circuit Type: Circuits::OCHCC_OVER_SUPER_CHANNEL_CIRCUIT*
Circuit Dir: Bidirectional
Circuit SLA: 0
Circuit OchncChannel: 191.35(1566_72) [freq=19135000/dim=5000]
Circuit OchncDir: 0
Circuit isGmpls: true
*Circuit GmplsInfo.ServiceId: ad8a0165:25*
Circuit GmplsInfo.AllowedOptOperZone: 4
Circuit GmplsInfo.IsUni: false
Circuit GmplsInfo.restorationMode: 0
Circuit GmplsInfo.revertiveMode: 0
Circuit GmplsInfo.revertiveSoakTime: 60
Circuit GmplsInfo.restorationStatus: 0
Circuit GmplsInfo.operZone: 3
Circuit GmplsInfo.priority: 0
Circuit GmplsInfo.circuitLabel: T3A_100G_SD20
Circuit GmplsInfo.downstreamPwrOffset: 0
Circuit GmplsInfo.upstreamPwrOffset: 0
Circuit GmplsInfo.allowRegens: 0
Circuit GmplsInfo.isRegenerated: 0
Circuit GmplsInfo.ochncChannels [n: 1]
   0] 191.35(1566_72) [1]
Circuit GmplsInfo.diversity [n: 0]
Circuit GmplsInfo.wsonSignalingVersion: Circuits::WSON_VER_UPGRADED
Circuit Flags: 0x0
  --> numb of sources[1]

*Circuit Source #0:   NodeId=ad8a0165*
   PortNum=0x5002
   StsNum=0
   VtNum=0
   OduNum=0
   memberNumber=0
   DropProtection=FALSE
  --> numb of drops[1]

*Circuit Drop #0:   NodeId=ad8a0169*
   PortNum=0x5002
   StsNum=0
   VtNum=0
   OduNum=0
   memberNumber=0
   DropProtection=FALSE
   PathProtection=Path Unprotected


FULL data is below

Cmd: |dumpCircuitList| ()

Circuit object list:
End of Circuit List
done
05:14:21 telnet-1@tcc102> Cmd: |dumpCircuitList| ()

Circuit object list:
End of Circuit List
done
05:14:23 telnet-1@tcc104> Cmd: |dumpCircuitList| ()

Circuit object list:
End of Circuit List
done
05:14:25 telnet-1@tcc103> Cmd: |dumpCircuitList| ()

Circuit object list:

Circuit ID: ad8a0165:26
Circuit Name: TRAIL-T3A_100G_SD20
Circuit Version: 0
Circuit Monitor: false
Circuit Size: OCHNC-NOSPEC
Circuit Type: Circuits::SUPER_CHANNEL_TRAIL_CIRCUIT
Circuit Dir: Bidirectional
Circuit SLA: 0
Circuit OchncChannel: 191.35(1566_72) [freq=19135000/dim=5000]
Circuit OchncDir: 0
Circuit isGmpls: true
Circuit GmplsInfo.ServiceId: ad8a0165:26
Circuit GmplsInfo.AllowedOptOperZone: 4
Circuit GmplsInfo.IsUni: false
Circuit GmplsInfo.restorationMode: 0
Circuit GmplsInfo.revertiveMode: 0
Circuit GmplsInfo.revertiveSoakTime: 60
Circuit GmplsInfo.restorationStatus: 0
Circuit GmplsInfo.operZone: 3
Circuit GmplsInfo.priority: 0
Circuit GmplsInfo.circuitLabel: T3A_100G_SD20
Circuit GmplsInfo.downstreamPwrOffset: 0
Circuit GmplsInfo.upstreamPwrOffset: 0
Circuit GmplsInfo.allowRegens: 0
Circuit GmplsInfo.isRegenerated: 0
Circuit GmplsInfo.ochncChannels [n: 1]
   0] 191.35(1566_72) [1]
Circuit GmplsInfo.diversity [n: 0]
Circuit GmplsInfo.wsonSignalingVersion: Circuits::WSON_VER_UPGRADED
Circuit Flags: 0x0
  --> numb of sources[1]
Circuit Source #0:
   NodeId=ad8a0165
   PortNum=0x5003
   StsNum=0
   VtNum=0
   OduNum=0
   memberNumber=0
   DropProtection=FALSE
  --> numb of drops[1]
Circuit Drop #0:
   NodeId=ad8a0169
   PortNum=0x5003
   StsNum=0
   VtNum=0
   OduNum=0
   memberNumber=0
   DropProtection=FALSE
   PathProtection=Path Unprotected

Circuit ID: ad8a0165:27
Circuit Name: TRAIL-T3A_100G_SD20
Circuit Version: 0
Circuit Monitor: false
Circuit Size: OCHNC-NOSPEC
Circuit Type: Circuits::CARRIER_TRAIL_CIRCUIT
Circuit Dir: Bidirectional
Circuit SLA: 0
Circuit OchncChannel: 191350.00-32.20 [freq=19135000/dim=3220]
Circuit OchncDir: 0
Circuit isGmpls: true
Circuit GmplsInfo.ServiceId: ad8a0165:26
Circuit GmplsInfo.AllowedOptOperZone: 4
Circuit GmplsInfo.IsUni: false
Circuit GmplsInfo.restorationMode: 0
Circuit GmplsInfo.revertiveMode: 0
Circuit GmplsInfo.revertiveSoakTime: 60
Circuit GmplsInfo.restorationStatus: 0
Circuit GmplsInfo.operZone: 3
Circuit GmplsInfo.priority: 0
Circuit GmplsInfo.circuitLabel: T3A_100G_SD20_01
Circuit GmplsInfo.downstreamPwrOffset: 0
Circuit GmplsInfo.upstreamPwrOffset: 0
Circuit GmplsInfo.allowRegens: 0
Circuit GmplsInfo.isRegenerated: 0
Circuit GmplsInfo.ochncChannels [n: 1]
   0] 191350.00-32.20 [1]
Circuit GmplsInfo.diversity [n: 0]
Circuit GmplsInfo.wsonSignalingVersion: Circuits::WSON_VER_UPGRADED
Circuit Flags: 0x0
  --> numb of sources[1]
Circuit Source #0:
   NodeId=ad8a0165
   PortNum=0x5003
   StsNum=0
   VtNum=0
   OduNum=0
   memberNumber=0
   DropProtection=FALSE
  --> numb of drops[1]
Circuit Drop #0:
   NodeId=ad8a0169
   PortNum=0x5003
   StsNum=0
   VtNum=0
   OduNum=0
   memberNumber=0
   DropProtection=FALSE
   PathProtection=Path Unprotected

Circuit ID: ad8a0165:25
*Circuit Name: T3A_100G_SD20*
Circuit Version: 0
Circuit Monitor: false
Circuit Size: OCH-TRAIL
*Circuit Type: Circuits::OCHCC_OVER_SUPER_CHANNEL_CIRCUIT*
Circuit Dir: Bidirectional
Circuit SLA: 0
Circuit OchncChannel: 191.35(1566_72) [freq=19135000/dim=5000]
Circu

Re: RegEx to match set of lines

2018-04-05 Thread Steven D'Aprano
On Thu, 05 Apr 2018 14:09:23 +0530, Prahallad Achar wrote:

> Hello,
> I would like to match set of lines from the below data. this data comes
> from one of the network

Good grief! You don't need to post your entire data set! We don't need or 
want to see hundreds of lines.

Cut your data down to a SMALL representative sample. Explain how it is 
coming to you: it looks like you are reading the data in something 
similar to key:value format. Is that correct?

If so, you don't need a regex. This is not Perl, we have more than one 
tool in our toolbox and don't have to force everything to be a regex.

The way I would process it would be to write a small accumulator function 
to group the lines into records, then a filter function to extract the 
records you want, and finally a function to extract the specific values 
you want from the record.

Far more understandable and maintainable, and less fragile, than a 
horribly complex regex.


-- 
Steve

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


want to export some of the packets from a big pacp file to another file.

2018-04-05 Thread supswain
Hi,

I am using dpkt python package to parse .pcap file and I am able to do 
successfully.

My requirement is to filter some of the traffic from the big .pcap file and to 
export the result to another file.

I don't know how to do this.

PFB the setup details I am using.
###
OS-windows 7 32 bit
python->2.7.6 32 bit


Highly appreciating your effort to help me.


Thanks,
Supriya
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: want to export some of the packets from a big pacp file to another file.

2018-04-05 Thread Anders Wegge Keller
På Thu, 5 Apr 2018 08:06:10 -0700 (PDT)
supsw...@gmail.com skrev:
> Hi,
> 
> I am using dpkt python package to parse .pcap file and I am able to do
> successfully.
> 
> My requirement is to filter some of the traffic from the big .pcap file
> and to export the result to another file.
> 
> I don't know how to do this.

 What kind of filtering do you need? In many cases it would be faster and
more convenient to use wireshark or other similar tools as a pass-through
filter, rather than rolling your own. 

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


Re: want to export some of the packets from a big pacp file to another file.

2018-04-05 Thread Grant Edwards
On 2018-04-05, supsw...@gmail.com  wrote:
> Hi,
>
> I am using dpkt python package to parse .pcap file and I am able to do 
> successfully.
>
> My requirement is to filter some of the traffic from the big .pcap
> file and to export the result to another file.
>
> I don't know how to do this.

The easiest way is to use tcpdump on the command line.

Let's say you've got a huge file (huge.pcap), and all you want to see is TCP 
traffic to/from 
10.0.0.104:

  tcpdump -r huge.pcap -w output.pcap tcp and host 10.0.0.104

If you insist on doing it in Python, then use can use pylibpcap to
read/parse the file.

https://sourceforge.net/projects/pylibpcap/files/pylibpcap/

When reading the file, you can use the normal capture filters that you
use with tcpdump.  Once you've read the packet, you can apply your own
logic if you want.  I don't recall ever trying to install it on
windows. It requires the pcap library, which is available for Windows.
I don't recall that it has methods to write a file, so you may have to
roll that bit yourself.

If you want to write something from scratch, here's the file format:

https://wiki.wireshark.org/Development/LibpcapFileFormat

You should be able to use ctypes to directly access the winpcap
library if you want to:

https://www.winpcap.org/

-- 
Grant Edwards   grant.b.edwardsYow! !  Up ahead!  It's a
  at   DONUT HUT!!
  gmail.com

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


Re: RegEx to match set of lines

2018-04-05 Thread Terry Reedy

On 4/5/2018 11:03 AM, Steven D'Aprano wrote:

On Thu, 05 Apr 2018 14:09:23 +0530, Prahallad Achar wrote:


I would like to match set of lines from the below data. this data comes
from one of the network


Good grief! You don't need to post your entire data set! We don't need or
want to see hundreds of lines.


+100


Cut your data down to a SMALL representative sample. Explain how it is
coming to you: it looks like you are reading the data in something
similar to key:value format. Is that correct?

If so, you don't need a regex. This is not Perl, we have more than one
tool in our toolbox and don't have to force everything to be a regex.


+10
Your bloated post will go to at least 10 archives and some unknown 1000s 
of people.



The way I would process it would be to write a small accumulator function
to group the lines into records, then a filter function to extract the
records you want, and finally a function to extract the specific values
you want from the record.


In Python, I would write a generator to yield records (groups of lines 
pertaining to one circuit.  It appears that each group starts with a 
circuid ID and ends with a blank line.  Then


for record in file:
if wanted(record):
process(record)


Far more understandable and maintainable, and less fragile, than a
horribly complex regex.


I have done this sort of thing in C, but mixing together the code for 
separating, identifying, and processing records made it less transparent 
than the above.


--
Terry Jan Reedy

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


Re: logging.FileHandler diff Py2 v Py3

2018-04-05 Thread Skip Montanaro
> I'll poke around a little and maybe open a bug report if I can't find any
> explanation for the change in behavior.

Turns out to be a known problem with a bit of history:

https://bugs.python.org/issue27805

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


try-except syntax

2018-04-05 Thread ElChino

I'm trying to simplify a try-except construct. E.g. how come
this:
  try:
_x, pathname, _y = imp.find_module (mod, mod_path)
return ("%s" % pathname)
  except ImportError:
pass
  except RuntimeError:
pass
return ("")

Cannot be simplified into this:
  try:
_x, pathname, _y = imp.find_module (mod, mod_path)
return ("%s" % pathname)
  except ImportError:
  except RuntimeError:
pass
return ("")

Like a "fall-through" in a C-switch statement.
--
https://mail.python.org/mailman/listinfo/python-list


Re: try-except syntax

2018-04-05 Thread Rob Gaddi

On 04/05/2018 02:04 PM, ElChino wrote:

I'm trying to simplify a try-except construct. E.g. how come
this:
   try:
     _x, pathname, _y = imp.find_module (mod, mod_path)
     return ("%s" % pathname)
   except ImportError:
     pass
   except RuntimeError:
     pass
     return ("")

Cannot be simplified into this:
   try:
     _x, pathname, _y = imp.find_module (mod, mod_path)
     return ("%s" % pathname)
   except ImportError:
   except RuntimeError:
     pass
     return ("")

Like a "fall-through" in a C-switch statement.


try:
  _x, pathname, _y = imp.find_module (mod, mod_path)
  return ("%s" % pathname)
except (ImportError, RuntimeError):
  pass
  return ("")

That handles the identical case.  C-style fall-throughs where you have 
one switch have just a bit of code before the fall-through kicks in (the 
slightly non-identical case) is often the source of disastrous code errors.


--
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.
--
https://mail.python.org/mailman/listinfo/python-list


Re: try-except syntax

2018-04-05 Thread Ian Kelly
On Thu, Apr 5, 2018 at 3:04 PM, ElChino  wrote:
> I'm trying to simplify a try-except construct. E.g. how come
> this:
>   try:
> _x, pathname, _y = imp.find_module (mod, mod_path)
> return ("%s" % pathname)
>   except ImportError:
> pass
>   except RuntimeError:
> pass
> return ("")
>
> Cannot be simplified into this:
>   try:
> _x, pathname, _y = imp.find_module (mod, mod_path)
> return ("%s" % pathname)
>   except ImportError:
>   except RuntimeError:
> pass
> return ("")
>
> Like a "fall-through" in a C-switch statement.

You're looking for:

  try:
_x, pathname, _y = imp.find_module (mod, mod_path)
return ("%s" % pathname)
  except (ImportError, RuntimeError):
return ("")
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: try-except syntax

2018-04-05 Thread Ben Bacarisse
ElChino  writes:

> I'm trying to simplify a try-except construct. E.g. how come
> this:
>   try:
> _x, pathname, _y = imp.find_module (mod, mod_path)
> return ("%s" % pathname)
>   except ImportError:
> pass
>   except RuntimeError:
> pass
> return ("")
>
> Cannot be simplified into this:
>   try:
> _x, pathname, _y = imp.find_module (mod, mod_path)
> return ("%s" % pathname)
>   except ImportError:
>   except RuntimeError:
> pass
> return ("")
>
> Like a "fall-through" in a C-switch statement.

The except parts don't fall through, and each one needs a "suite".
Also, the "pass" before the return is mysterious which makes me think
there's an indent error in what you posted.

Anyway, to coalesce two or more exception handlers, you are probably
looking for

  except (ImportError, RuntimeError):

which matches both.

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


Re: try-except syntax

2018-04-05 Thread Gregory Ewing

Ben Bacarisse wrote:

Anyway, to coalesce two or more exception handlers, you are probably
looking for

  except (ImportError, RuntimeError):


To the OP: Are you sure you really want to mask RuntimeError here?
Usually it means something has gone seriously wrong, and is a
symptom that shouldn't be ignored.

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


install MySQL-python failed ....

2018-04-05 Thread yi zhao
$ python -m pip install MySQL-python
Collecting MySQL-python
  Downloading MySQL-python-1.2.5.zip (108kB)
100% || 112kB 260kB/s
930 [main] python2.7 12948 child_info_fork::abort: address space needed by 
'datetime.dll' (0x87) is already occupied
Error [Errno 11] Resource temporarily unavailable while executing command 
python setup.py egg_info
Exception:
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/pip/basecommand.py", line 215, in main
status = self.run(options, args)
  File "/usr/lib/python2.7/site-packages/pip/commands/install.py", line 324, in 
run
requirement_set.prepare_files(finder)
  File "/usr/lib/python2.7/site-packages/pip/req/req_set.py", line 380, in 
prepare_files
ignore_dependencies=self.ignore_dependencies))
  File "/usr/lib/python2.7/site-packages/pip/req/req_set.py", line 634, in 
_prepare_file
abstract_dist.prep_for_dist()
  File "/usr/lib/python2.7/site-packages/pip/req/req_set.py", line 129, in 
prep_for_dist
self.req_to_install.run_egg_info()
  File "/usr/lib/python2.7/site-packages/pip/req/req_install.py", line 439, in 
run_egg_info
command_desc='python setup.py egg_info')
  File "/usr/lib/python2.7/site-packages/pip/utils/__init__.py", line 667, in 
call_subprocess
cwd=cwd, env=env)
  File "/usr/lib/python2.7/subprocess.py", line 390, in __init__
errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 917, in _execute_child
self.pid = os.fork()
OSError: [Errno 11] Resource temporarily unavailable


anything hit similar issue?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: install MySQL-python failed ....

2018-04-05 Thread Larry Martell
On Thu, Apr 5, 2018 at 7:12 PM, yi zhao  wrote:
> $ python -m pip install MySQL-python
> Collecting MySQL-python
>   Downloading MySQL-python-1.2.5.zip (108kB)
> 100% || 112kB 260kB/s
> 930 [main] python2.7 12948 child_info_fork::abort: address space needed 
> by 'datetime.dll' (0x87) is already occupied
> Error [Errno 11] Resource temporarily unavailable while executing command 
> python setup.py egg_info
> Exception:
> Traceback (most recent call last):
>   File "/usr/lib/python2.7/site-packages/pip/basecommand.py", line 215, in 
> main
> status = self.run(options, args)
>   File "/usr/lib/python2.7/site-packages/pip/commands/install.py", line 324, 
> in run
> requirement_set.prepare_files(finder)
>   File "/usr/lib/python2.7/site-packages/pip/req/req_set.py", line 380, in 
> prepare_files
> ignore_dependencies=self.ignore_dependencies))
>   File "/usr/lib/python2.7/site-packages/pip/req/req_set.py", line 634, in 
> _prepare_file
> abstract_dist.prep_for_dist()
>   File "/usr/lib/python2.7/site-packages/pip/req/req_set.py", line 129, in 
> prep_for_dist
> self.req_to_install.run_egg_info()
>   File "/usr/lib/python2.7/site-packages/pip/req/req_install.py", line 439, 
> in run_egg_info
> command_desc='python setup.py egg_info')
>   File "/usr/lib/python2.7/site-packages/pip/utils/__init__.py", line 667, in 
> call_subprocess
> cwd=cwd, env=env)
>   File "/usr/lib/python2.7/subprocess.py", line 390, in __init__
> errread, errwrite)
>   File "/usr/lib/python2.7/subprocess.py", line 917, in _execute_child
> self.pid = os.fork()
> OSError: [Errno 11] Resource temporarily unavailable
>
>
> anything hit similar issue?

Is this happening in cygwin? Or in a native linux?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: install MySQL-python failed ....

2018-04-05 Thread INADA Naoki
https://stackoverflow.com/questions/11107155/how-to-fix-address-space-is-already-occupied-error-on-fetch-commit

On Fri, Apr 6, 2018 at 8:12 AM, yi zhao  wrote:
> $ python -m pip install MySQL-python
> Collecting MySQL-python
>   Downloading MySQL-python-1.2.5.zip (108kB)
> 100% || 112kB 260kB/s
> 930 [main] python2.7 12948 child_info_fork::abort: address space needed 
> by 'datetime.dll' (0x87) is already occupied
> Error [Errno 11] Resource temporarily unavailable while executing command 
> python setup.py egg_info
> Exception:
> Traceback (most recent call last):
>   File "/usr/lib/python2.7/site-packages/pip/basecommand.py", line 215, in 
> main
> status = self.run(options, args)
>   File "/usr/lib/python2.7/site-packages/pip/commands/install.py", line 324, 
> in run
> requirement_set.prepare_files(finder)
>   File "/usr/lib/python2.7/site-packages/pip/req/req_set.py", line 380, in 
> prepare_files
> ignore_dependencies=self.ignore_dependencies))
>   File "/usr/lib/python2.7/site-packages/pip/req/req_set.py", line 634, in 
> _prepare_file
> abstract_dist.prep_for_dist()
>   File "/usr/lib/python2.7/site-packages/pip/req/req_set.py", line 129, in 
> prep_for_dist
> self.req_to_install.run_egg_info()
>   File "/usr/lib/python2.7/site-packages/pip/req/req_install.py", line 439, 
> in run_egg_info
> command_desc='python setup.py egg_info')
>   File "/usr/lib/python2.7/site-packages/pip/utils/__init__.py", line 667, in 
> call_subprocess
> cwd=cwd, env=env)
>   File "/usr/lib/python2.7/subprocess.py", line 390, in __init__
> errread, errwrite)
>   File "/usr/lib/python2.7/subprocess.py", line 917, in _execute_child
> self.pid = os.fork()
> OSError: [Errno 11] Resource temporarily unavailable
>
>
> anything hit similar issue?
> --
> https://mail.python.org/mailman/listinfo/python-list



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


Re: try-except syntax

2018-04-05 Thread Steven D'Aprano
On Thu, 05 Apr 2018 23:04:18 +0200, ElChino wrote:

> I'm trying to simplify a try-except construct. E.g. how come this:
>try:
>  _x, pathname, _y = imp.find_module (mod, mod_path)
>  return ("%s" % pathname)

imp.find_module is deprecated and should not be used in new code.

Putting that aside, pathname is already a string. Why are you wasting 
time interpolating it into a bare "%s" string? Just say `return pathname`.


>except ImportError:
>  pass
>except RuntimeError:
>  pass
>  return ("")

Unnecessary parens surrounding a single value.

Possible indentation error. Surely the return "" needs to be 
indented level with the try/except statements?


> Cannot be simplified into this:
>try:
>  _x, pathname, _y = imp.find_module (mod, mod_path) return ("%s" %
>  pathname)
>except ImportError:
>except RuntimeError:
>  pass
>  return ("")
> 
> Like a "fall-through" in a C-switch statement.

Because fall-through C switch syntax is an abomination, and fortunately 
the designer of Python has more sense than to allow that awfulness.


The syntax you are looking for is:


try:
  block
except (ImportError, RuntimeError):
  block



By the way, RuntimeError is almost never something you want to catch 
(except to log before bailing out). It should represent a fatal coding 
error, not something safe to ignore.


-- 
Steve

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


Re: # of Months between two dates

2018-04-05 Thread Jason Friedman
>
> > > I've written a function to return the months between date1 and date2
> but
> > > I'd like to know if anyone is aware of anything in the standard library
> > > to do the same?  For bonus points, does anyone know if postgres can do
> > > the same (we use a lot of date/time funcitons in postgres, already, but
> > > didn't see this problem addressed).
>


> > It's probably better to write the function yourself according to what
> > makes sense in your use-case, and document its behaviour clearly.
>

I suggest using the dateutil module (
https://pypi.python.org/pypi/python-dateutil) before writing your own.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: # of Months between two dates

2018-04-05 Thread Steven D'Aprano
On Thu, 05 Apr 2018 22:03:07 -0600, Jason Friedman wrote:


>> > > I've written a function to return the months between date1 and
>> > > date2
>> but
>> > > I'd like to know if anyone is aware of anything in the standard
>> > > library to do the same?  For bonus points, does anyone know if
>> > > postgres can do the same (we use a lot of date/time funcitons in
>> > > postgres, already, but didn't see this problem addressed).
>>
>>
> 
>> > It's probably better to write the function yourself according to what
>> > makes sense in your use-case, and document its behaviour clearly.
>>
>>
> I suggest using the dateutil module (
> https://pypi.python.org/pypi/python-dateutil) before writing your own.



I'm not seeing a "months between" function in dateutil. Have I missed 
something?


The question of how many months are between two dates is hard to answer. 
For example:

- between Jan 1 and Feb 1 is clearly one month (31 days);

- unless you think all months should be 30 days, in which case
  its one month and one day;

- giving one month from Jan 1 as Jan 31, which seems weird;

- either way, the period between Feb 1 and Mar 1 is only four
  weeks and so obviously less than one month;

- unless you think a month is four weeks precisely;

- in which case it is one month between Jan 1 and Jan 29;

- one month forward from Jun 30 is clearly Jul 30;

- but one month back from Jul 31 is either Jun 30 or Jul 1;

- instead of counting days, with all the difficulty that
  causes, we could just count how many times the month
  changes;

- in which case, Jan 31 to Feb 1 is one month.



I wish the calendar designers had made the year 13 months of exactly 4 
weeks each, with one intercalary day left over (two in leap years). Half 
a year becomes exactly 6 months and two weeks; a quarter of a year 
becomes three months and one week. A third is a bit more than four weeks. 
Date calculations would be much simpler, with only one (or two in leap 
years) special case, the intercalary day, instead of 36.



-- 
Steve

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