Re: help: pandas and 2d table

2024-04-13 Thread Tim Williams via Python-list
On Sat, Apr 13, 2024 at 1:10 PM Mats Wichmann via Python-list <
python-list@python.org> wrote:

> On 4/13/24 07:00, jak via Python-list wrote:
>
> doesn't Pandas have a "where" method that can do this kind of thing? Or
> doesn't it match what you are looking for?  Pretty sure numpy does, but
> that's a lot to bring in if you don't need the rest of numpy.
>
> pandas.DataFrame.where — pandas 2.2.2 documentation (pydata.org)

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


Re: Is there a Python module to parse a date like the 'date' command in Linux?

2023-05-22 Thread Tim Williams
On Mon, May 22, 2023 at 12:41 PM Mats Wichmann  wrote:

> On 5/20/23 13:53, Chris Green wrote:
> > I'm converting a bash script to python as it has become rather clumsy
> > in bash.
> >
> > However I have hit a problem with converting dates, the bash script
> > has:-
> >
> >  dat=$(date --date "$1" +"%Y/%m/%d")
> >
> > and this will accept almost anything reasonably sensible that can be
> > interpreted as a date, in particular it accepts things like "tomorrow",
> > "yesterday" and "next thursday".
> >
> > Is there anything similar in Python or would I be better off simply
> > using os.system() to run date from the python program?
> >
>
> in the standard library, datetime
>
> as an addon module, dateutil  (install as python-dateutil)
>
> Don't know if either are exactly what you want, but do take a look.
>
> --
> https://mail.python.org/mailman/listinfo/python-list


In particular,check out dateutil.parser.
parser — dateutil 2.8.2 documentation


parser


This module offers a generic date/time string parser which is able to parse
most known formats to represent a date and/or time.

This module attempts to be forgiving with regards to unlikely input
formats, returning a datetime object even for dates which are ambiguous. If
an element of a date/time stamp is omitted, the following rules are applied:
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to store the result of df.count() as a new dataframe in Pandas?

2021-10-27 Thread Tim Williams
On Tue, Oct 26, 2021 at 6:36 PM Shaozhong SHI 
wrote:

> Hello,
>
> The result of df.count() appears to be a series object.  How to store the
> result of df.count() as a new dataframe in Pandas?
>
> That is data anyhow.
>
> Regards,
>
> David
> --
> https://mail.python.org/mailman/listinfo/python-list




Have you tried something like

df_count = pd.DataFrame(df.count())
?
(Untested, but I've converted Series objects to DataFrames doing something
similar before.)
This is more of a pandas question. Why don't you ask this on stackoverflow?

https://stackoverflow.com/questions/tagged/pandas
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: ValueError: arrays must all be same length

2020-10-05 Thread Tim Williams
On Mon, Oct 5, 2020 at 6:47 AM Shaozhong SHI  wrote:

>
> Hi, I managed to flatten it with json_normalize first.
>
> from pandas.io.json import json_normalize
> atable = json_normalize(d)
> atable
>
> Then, I got this table.
>
> brandId brandName careHome constituency
> currentRatings.overall.keyQuestionRatings currentRatings.overall.rating
> currentRatings.overall.reportDate currentRatings.overall.reportLinkId
> currentRatings.reportDate dormancy ... providerId region registrationDate
> registrationStatus regulatedActivities relationships reports specialisms
> type uprn
> 0 BD510 BRAND MACC Care Y Birmingham, Northfield [{u'reportDate':
> u'2020-10-01', u'rating': u'R... Requires improvement 2020-10-01
> 1157c975-c2f1-423e-a2b4-66901779e014 2020-10-01 N ... 1-101641521 West
> Midlands 2013-12-16 Registered [{u'code': u'RA2', u'name':
> u'Accommodation
>
> Then, I tried to expand the column
> of currentRatings.overall.keyQuestionRatings, with
>
> mydf =
> pd.DataFrame.from_dict(atable['currentRatings.overall.keyQuestionRatings'][0])
> mydf
>
> Then, I got another table.
>
> name rating reportDate reportLinkId
> 0 Safe Requires improvement 2020-10-01
> 1157c975-c2f1-423e-a2b4-66901779e014
> 1 Well-led Requires improvement 2020-10-01
> 1157c975-c2f1-423e-a2b4-66901779e014
> 2 Caring Good 2019-10-04 63ff05ec-4d31-406e-83de-49a271cfdc43
> 3 Responsive Good 2019-10-04 63ff05ec-4d31-406e-83de-49a271cfdc43
> 4 Effective Requires improvement 2019-10-04
> 63ff05ec-4d31-406e-83de-49a271cfdc43
>
>
> How can I re-arrange to get a flatten table?
>
> Apparently, the nested data is another table.
>
> Regards,
>
> Shao
>
>
> I'm fairly new to pandas myself. Can't help there. You may want to post
this on Stackoverflow, or look for a similar issue on github.

https://stackoverflow.com/questions/tagged/pandas+json
https://github.com/pandas-dev/pandas/issues




>
> On Sun, 4 Oct 2020 at 13:55, Tim Williams  wrote:
>
>> On Sun, Oct 4, 2020 at 8:39 AM Tim Williams  wrote:
>>
>> >
>> >
>> > On Fri, Oct 2, 2020 at 11:00 AM Shaozhong SHI 
>> > wrote:
>> >
>> >> Hello,
>> >>
>> >> I got a json response from an API and tried to use pandas to put data
>> into
>> >> a dataframe.
>> >>
>> >> However, I kept getting this ValueError: arrays must all be same
>> length.
>> >>
>> >> Can anyone help?
>> >>
>> >> The following is the json text.  Regards, Shao
>> >>
>> >> (snip json_text)
>> >
>> >
>> >> import pandas as pd
>> >>
>> >> import json
>> >>
>> >> j = json.JSONDecoder().decode(req.text)  ###req.json
>> >>
>> >> df = pd.DataFrame.from_dict(j)
>> >>
>> >
>> > I copied json_text into a Jupyter notebook and got the same error trying
>> > to convert this into a pandas DataFrame:When I tried to copy this into a
>> > string, I got an error,, but without enclosing the paste in quotes, I
>> got
>> > the dictionary.
>> >
>> >
>> (delete long response output)
>>
>>
>> > for k in json_text.keys():
>> > if isinstance(json_text[k], list):
>> > print(k, len(json_text[k]))
>> >
>> > relationships 0
>> > locationTypes 0
>> > regulatedActivities 2
>> > gacServiceTypes 1
>> > inspectionCategories 1
>> > specialisms 4
>> > inspectionAreas 0
>> > historicRatings 4
>> > reports 5
>> >
>> > HTH,.
>> >
>> >
>> This may also be more of a pandas issue.
>>
>> json.loads(json.dumps(json_text))
>>
>> has a successful round-trip
>>
>>
>> > --
>> >> https://mail.python.org/mailman/listinfo/python-list
>> >>
>> >
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: ValueError: arrays must all be same length

2020-10-04 Thread Tim Williams
On Sun, Oct 4, 2020 at 8:39 AM Tim Williams  wrote:

>
>
> On Fri, Oct 2, 2020 at 11:00 AM Shaozhong SHI 
> wrote:
>
>> Hello,
>>
>> I got a json response from an API and tried to use pandas to put data into
>> a dataframe.
>>
>> However, I kept getting this ValueError: arrays must all be same length.
>>
>> Can anyone help?
>>
>> The following is the json text.  Regards, Shao
>>
>> (snip json_text)
>
>
>> import pandas as pd
>>
>> import json
>>
>> j = json.JSONDecoder().decode(req.text)  ###req.json
>>
>> df = pd.DataFrame.from_dict(j)
>>
>
> I copied json_text into a Jupyter notebook and got the same error trying
> to convert this into a pandas DataFrame:When I tried to copy this into a
> string, I got an error,, but without enclosing the paste in quotes, I got
> the dictionary.
>
>
(delete long response output)


> for k in json_text.keys():
> if isinstance(json_text[k], list):
> print(k, len(json_text[k]))
>
> relationships 0
> locationTypes 0
> regulatedActivities 2
> gacServiceTypes 1
> inspectionCategories 1
> specialisms 4
> inspectionAreas 0
> historicRatings 4
> reports 5
>
> HTH,.
>
>
This may also be more of a pandas issue.

json.loads(json.dumps(json_text))

has a successful round-trip


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


Re: ValueError: arrays must all be same length

2020-10-04 Thread Tim Williams
On Fri, Oct 2, 2020 at 11:00 AM Shaozhong SHI 
wrote:

> Hello,
>
> I got a json response from an API and tried to use pandas to put data into
> a dataframe.
>
> However, I kept getting this ValueError: arrays must all be same length.
>
> Can anyone help?
>
> The following is the json text.  Regards, Shao
>
> (snip json_text)


> import pandas as pd
>
> import json
>
> j = json.JSONDecoder().decode(req.text)  ###req.json
>
> df = pd.DataFrame.from_dict(j)
>

I copied json_text into a Jupyter notebook and got the same error trying to
convert this into a pandas DataFrame:When I tried to copy this into a
string, I got an error,, but without enclosing the paste in quotes, I got
the dictionary.

dir(json_text)
['__class__',
 '__contains__',
 '__delattr__',
 '__delitem__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__iter__',
 '__le__',
 '__len__',
 '__lt__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__reversed__',
 '__setattr__',
 '__setitem__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'clear',
 'copy',
 'fromkeys',
 'get',
 'items',
 'keys',
 'pop',
 'popitem',
 'setdefault',
 'update',
 'values']

pd.DataFrame(json_text)

---

ValueErrorTraceback (most recent call last)
 in 
> 1 pd.DataFrame(json_text)

D:\anaconda3\lib\site-packages\pandas\core\frame.py in __init__(self, data,
index, columns, dtype, copy)
433 )
434 elif isinstance(data, dict):
--> 435 mgr = init_dict(data, index, columns, dtype=dtype)
436 elif isinstance(data, ma.MaskedArray):
437 import numpy.ma.mrecords as mrecords

D:\anaconda3\lib\site-packages\pandas\core\internals\construction.py in
init_dict(data, index, columns, dtype)
252 arr if not is_datetime64tz_dtype(arr) else arr.copy()
for arr in arrays
253 ]
--> 254 return arrays_to_mgr(arrays, data_names, index, columns,
dtype=dtype)
255
256

D:\anaconda3\lib\site-packages\pandas\core\internals\construction.py in
arrays_to_mgr(arrays, arr_names, index, columns, dtype)
 62 # figure out the index, if necessary
 63 if index is None:
---> 64 index = extract_index(arrays)
 65 else:
 66 index = ensure_index(index)

D:\anaconda3\lib\site-packages\pandas\core\internals\construction.py in
extract_index(data)
363 lengths = list(set(raw_lengths))
364 if len(lengths) > 1:
--> 365 raise ValueError("arrays must all be same length")
366
367 if have_dicts:

ValueError: arrays must all be same length


I got a different error trying json.loads(str(json_text)),
---
JSONDecodeError   Traceback (most recent call last)
 in 
> 1 json.loads(str(json_text))

D:\anaconda3\lib\json\__init__.py in loads(s, cls, object_hook,
parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
355 parse_int is None and parse_float is None and
356 parse_constant is None and object_pairs_hook is None
and not kw):
--> 357 return _default_decoder.decode(s)
358 if cls is None:
359 cls = JSONDecoder

D:\anaconda3\lib\json\decoder.py in decode(self, s, _w)
335
336 """
--> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
338 end = _w(s, end).end()
339 if end != len(s):

D:\anaconda3\lib\json\decoder.py in raw_decode(self, s, idx)
351 """
352 try:
--> 353 obj, end = self.scan_once(s, idx)
354 except StopIteration as err:
355 raise JSONDecodeError("Expecting value", s, err.value)
from None

JSONDecodeError: Expecting property name enclosed in double quotes: line 1
column 2 (char 1)

I think the solution is to fix the arrays so that the lengths match.

for k in json_text.keys():
if isinstance(json_text[k], list):
print(k, len(json_text[k]))

relationships 0
locationTypes 0
regulatedActivities 2
gacServiceTypes 1
inspectionCategories 1
specialisms 4
inspectionAreas 0
historicRatings 4
reports 5

HTH,.

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


Re: Python Pandas split Date in day month year and hour

2020-08-19 Thread Tim Williams
On Wed, Aug 19, 2020 at 1:43 PM J Conrado  wrote:

>
>
> Hi,
>
>
> I'm satarting using Pandas to read excel. I have a meteorological
> synoptic data and I have for date:
>
>
> 0   2017-11-01 00:00:00
> 1   2017-11-01 03:00:00
> 2   2017-11-01 06:00:00
> 3   2017-11-01 09:00:00
> 4   2017-11-01 12:00:00
> ..  ...
> 229 2017-11-30 09:00:00
> 230 2017-11-30 12:00:00
> 231 2017-11-30 15:00:00
> 232 2017-11-30 18:00:00
> 233 2017-11-30 21:00:00
>
>
> I would like know how can I get for this array the values for day, month
> and hour:
>
> 2017-11-01 03:00:00   year = 2017  month = 11day = 1and
> hour = 3
>
>
>
> Thanks,
>
>
> Conrado
>
> --
> https://mail.python.org/mailman/listinfo/python-list


I'm also starting to learn pandas. A better place to ask a pandas question
is StackOverflow. Here's a link that may answer your question.

Convert timestamp to day, month, year and hour
<https://stackoverflow.com/questions/57515291/convert-timestamp-to-day-month-year-and-hour>


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


Re: conda/anaconda and pip3 (pip)

2018-12-09 Thread Tim Williams
On Saturday, December 8, 2018 at 10:13:14 PM UTC-5, Monte Milanuk wrote:
> Did you find any solution(s)?

I usually just lurk and read on this list. I don't reply since there's usually 
more competent people that regularly post helpful answers. (I lurk to learn 
from them!)

If no one's replied yet, I'll give it my 2 cents ...

Without being a pip expert, I see from 'pip install -h' that you can specify 
where you want the package to be installed.

Install Options:
  -r, --requirement Install from the given requirements file. This 
option can be used multiple times.
  -c, --constraint  Constrain versions using the given constraints 
file. This option can be used multiple
  times.
  --no-deps   Don't install package dependencies.
  --pre   Include pre-release and development versions. By 
default, pip only finds stable
  versions.
  -e, --editableInstall a project in editable mode (i.e. 
setuptools "develop mode") from a local project
  path or a VCS url.
  -t, --target   Install packages into . By default this will 
not replace existing files/folders in
  . Use --upgrade to replace existing packages 
in  with new versions.
  --user  Install to the Python user install directory for 
your platform. Typically ~/.local/, or
  %APPDATA%\Python on Windows. (See the Python 
documentation for site.USER_BASE for full
  details.)
  --root Install everything relative to this alternate 
root directory.
  --prefix   Installation prefix where lib, bin and other 
top-level folders are placed
  -b, --buildDirectory to unpack packages into and build in. 
Note that an initial build still takes
  place in a temporary directory. The location of 
temporary directories can be controlled
  by setting the TMPDIR environment variable (TEMP 
on Windows) appropriately. When passed,
  build directories are not cleaned in case of 
failures.
  --src  Directory to check out editable projects into. 
The default in a virtualenv is "/src". The default for global installs is 
"/src".
  -U, --upgrade   Upgrade all specified packages to the newest 
available version. The handling of
  dependencies depends on the upgrade-strategy used.
  --upgrade-strategy 
  Determines how dependency upgrading should be 
handled [default: only-if-needed]. "eager"
  - dependencies are upgraded regardless of whether 
the currently installed version
  satisfies the requirements of the upgraded 
package(s). "only-if-needed" -  are upgraded
  only when they do not satisfy the requirements of 
the upgraded package(s).
  --force-reinstall   Reinstall all packages even if they are already 
up-to-date.
  -I, --ignore-installed  Ignore the installed packages (reinstalling 
instead).
  --ignore-requires-pythonIgnore the Requires-Python information.
  --no-build-isolationDisable isolation when building a modern source 
distribution. Build dependencies
  specified by PEP 518 must be already installed if 
this option is used.
  --install-option   Extra arguments to be supplied to the setup.py 
install command (use like --install-
  option="--install-scripts=/usr/local/bin"). Use 
multiple --install-option options to
  pass multiple options to setup.py install. If you 
are using an option with a directory
  path, be sure to use absolute path.
  --global-optionExtra global options to be supplied to the 
setup.py call before the install command.
  --compile   Compile Python source files to bytecode
  --no-compileDo not compile Python source files to bytecode
  --no-warn-script-location   Do not warn when installing scripts outside PATH
  --no-warn-conflicts Do not warn about broken dependencies
  --no-binary 
  Do not use binary packages. Can be supplied 
multiple times, and each time adds to the
  existing value. Accepts either :all: to disable 
all binary packages, :none: to empty the
  set, or one or more package names with commas 
between them. Note that some packages are
  tricky to compile and may fail to install when 
this option is used on them.
  --only-binary 
  Do not use source packages. Can be supplied 
multiple times, and each time adds to the
  existing value. Accepts either :all: to disable 

Re: Is there a nice way to switch between 2 different packages providing the same APIs?

2018-07-05 Thread Tim Williams
On Thu, Jul 5, 2018 at 9:02 AM Mark Summerfield via Python-list <
python-list@python.org> wrote:

> For GUI programming I often use Python bindings for Qt.
>
> There are two competing bindings, PySide and PyQt.
>
> Ideally I like to have applications that can use either. This way, if I
> get a problem I can try with the other bindings: if I still get the
> problem, then it is probably me; but if I don't it may be an issue with the
> bindings.
>
> But working with both means that my imports are very messy. Here's a tiny
> example:
>
> if PYSIDE: # bool True -> Use PySide; False -> Use PyQt5
> from PySide2.QtCore import Qt
> from PySide2.QtGui import QIcon
> from PySide2.QtWidgets import (
> QDialog, QFrame, QGridLayout, QLabel, QLineEdit, QPushButton,
> QVBoxLayout)
> else:
> from PyQt5.QtCore import Qt
> from PyQt5.QtGui import QIcon
> from PyQt5.QtWidgets import (
> QDialog, QFrame, QGridLayout, QLabel, QLineEdit, QPushButton,
> QVBoxLayout)
>
> The PYSIDE constant is imported from another module and is used for all
> .py files in a given project so that just by changing PYSIDE's value I can
> run an entire application with PySide2 or with PyQt5.
>
> But I'd really rather just have one lot of imports per file.
>
> One obvious solution is to create a 'Qt.py' module that imports everything
> depending on the PYSIDE switch and that I then use in all the other .py
> files, something like this:
>
> from Qt.QtCore import Qt
> from Qt.QtGui import QIcon
> ... etc.
>
> But I'm just wondering if there's a nicer way to do all this?
> --
> https://mail.python.org/mailman/listinfo/python-list


Check out pyqtgraph 

It tries to use  PyQt5/PyQt4/PySide depending on which if these packages
were imported before importing pyqtgraph.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Problem with assignment. Python error or mine?

2017-12-22 Thread Tim Williams
On Friday, December 22, 2017 at 8:41:29 AM UTC-5, Tim Williams wrote:
> On Thursday, December 21, 2017 at 12:18:11 PM UTC-5, MarkA wrote:
> > On Thu, 21 Dec 2017 07:05:33 -0800, rafaeltfreire wrote:
> > From docs.python.org:
> > 
> > 8.10. copy — Shallow and deep copy operations
> > 
> > Source code: Lib/copy.py
> > 
> > Assignment statements in Python do not copy objects, they create bindings 
> > between a target and an object. For collections that are mutable or 
> > contain mutable items, a copy is sometimes needed so one can change one 
> > copy without changing the other. This module provides generic shallow and 
> > deep copy operations (explained below)...
> > 
> > 
> > > Dear community, I am having the following problem when I am assigning
> > > the elements of a vector below a certain number to zero or any other
> > > value.
> > > I am creating a new variable but Python edits the root variable. Why?
> > > 
> > > import numpy as np
> > > 
> > > X=np.arange(1, 1, 1) #root variable x1=X x1[x1<1]=0
> > > 
> > > print(X)
> > > Out[1]: array([ 0.,  0.,  0., ...,  0.,  0.,  0.])
> > > 
> > > Why? It is supposed to be the original value Thank you for your
> > > time Rafael
> > 
> > 
> > 
> > -- 
> > MarkA
> > 
> > We hang petty theives, and appoint the great theives to public office
> >   -- Aesop
> 
> Shouldn't the OP just create a list for what he want's to do?
> 
> X = list(np.arange(1, 1, 1)) #root variable x1=X x1[x1<1]=0
> 
> Then I think his other statements would do what he expects, no?

Disregard what I just posted. I didn't think this through enough. 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Problem with assignment. Python error or mine?

2017-12-22 Thread Tim Williams
On Thursday, December 21, 2017 at 12:18:11 PM UTC-5, MarkA wrote:
> On Thu, 21 Dec 2017 07:05:33 -0800, rafaeltfreire wrote:
> From docs.python.org:
> 
> 8.10. copy — Shallow and deep copy operations
> 
> Source code: Lib/copy.py
> 
> Assignment statements in Python do not copy objects, they create bindings 
> between a target and an object. For collections that are mutable or 
> contain mutable items, a copy is sometimes needed so one can change one 
> copy without changing the other. This module provides generic shallow and 
> deep copy operations (explained below)...
> 
> 
> > Dear community, I am having the following problem when I am assigning
> > the elements of a vector below a certain number to zero or any other
> > value.
> > I am creating a new variable but Python edits the root variable. Why?
> > 
> > import numpy as np
> > 
> > X=np.arange(1, 1, 1) #root variable x1=X x1[x1<1]=0
> > 
> > print(X)
> > Out[1]: array([ 0.,  0.,  0., ...,  0.,  0.,  0.])
> > 
> > Why? It is supposed to be the original value Thank you for your
> > time Rafael
> 
> 
> 
> -- 
> MarkA
> 
> We hang petty theives, and appoint the great theives to public office
>   -- Aesop

Shouldn't the OP just create a list for what he want's to do?

X = list(np.arange(1, 1, 1)) #root variable x1=X x1[x1<1]=0

Then I think his other statements would do what he expects, no?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: matplot plot hangs

2017-11-02 Thread Tim Williams
On Wednesday, November 1, 2017 at 6:30:27 PM UTC-4, Andrew Z wrote:
> nope. it doesnt:
> 
> I added print-s after each line and that produced:
> [az@hp src]$ cat ./main1.py
> import matplotlib.pyplot as plt
> print("imported")
> plt.plot([1,2,4,1])
> print("plot is done")
> plt.show()
> print("show is done")
> 
> [az@hp src]$ python3.5 ./main1.py
> imported
> ^C^Z
> [1]+  Stopped python3.5 ./main1.py
> 
> 
> On Wed, Nov 1, 2017 at 9:31 AM, Vlastimil Brom 
> wrote:
> 
> > 2017-11-01 13:49 GMT+01:00 Andrew Z :
> > > Wolfgang,
> > >  I tried to ran from ide with no rwsults, so now im trying from a
> > terminal
> > > in xwindow.
> > > The .plot is the last line in the script and it does hang trying to
> > execute
> > > it.
> > >
> > >
> > > On Nov 1, 2017 05:44, "Wolfgang Maier" <
> > > wolfgang.ma...@biologie.uni-freiburg.de> wrote:
> > >
> > > On 01.11.2017 00:40, Andrew Z wrote:
> > >
> > >> hello,
> > >>   learning python's plotting by using matplotlib with python35 on
> > fedora 24
> > >> x86.
> > >>
> > >> Installed matplotlib into user's directory.
> > >> tk, seemed to work -
> > >> http://www.tkdocs.com/tutorial/install.html#installlinux - the window
> > >> shows
> > >> up just fine.
> > >> but when trying to run the simple plot (
> > >> https://matplotlib.org/examples/pylab_examples/simple_plot.html) the
> > >> script
> > >> is hanging on;
> > >>
> > >> plt.plot(t, s)
> > >>
> > >> attempts to
> > >> matplotlib.interactive(True) didn't bring anything,
> > >>
> > >>
> > > Hi Andrew,
> > >
> > > From which environment are you trying to run the example? In the
> > terminal,
> > > from within some IDE, inside a jupyter notebook?
> > >
> > > Are you sure the script "is hanging on plt.plot(t, s)" and not after
> > that?
> > >
> > > Best,
> > > Wolfgang
> > >
> > > --
> > Hi,
> > sorry if it is too trivial, just to make sure, do you have a call to
> > "show()" the resulting plot in the code?
> >
> > An elementary plotting code might be e.g.:
> >
> > import matplotlib.pyplot as plt
> > plt.plot([1,2,4,1])
> > plt.show()
> >
> > Does this work in your environment?
> >
> > It was not quite clear, what do you plan with interactive drawing, or
> > whether you are using e.g. plt.interactive(True) already - this might
> > be a problem as there could be collisions or the plot window is closed
> > after the standalone script finishes.
> >
> > hth,
> >  vbr
> > --
> > https://mail.python.org/mailman/listinfo/python-list
> >

Have you tried 

plt.show(block=False)
?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: using configobj string interpolation and logging.config.dictConfig

2017-05-30 Thread Tim Williams
On Friday, May 26, 2017 at 12:26:13 PM UTC-4, Peter Otten wrote:
> Tim Williams wrote:
> 
> > I've spent too much time trying to track this down. I'll just hard-code my
> > filename in my INI file. Maybe I'll get back to it, but I need to move on.
> 
> The only alternative I see would be to build your own InterpolationEngine 
> which understands some kind of escaping so that e. g.
> 
> format = '%%(asctime)s: (%%(levelname)s)  %%(message)s'
> 
> would become
> 
> format = '%(asctime)s: (%(levelname)s)  %(message)s'
> 
> when you invoke the dict() method.

It helps to sometimes just let a problem rest for awhile. I found a workaround 
to my problem. In my INI file, don't create a 'filename' key in the 
config['handlers']['file'] section. Set it in my python code after loading the 
INI file. The following works just fine for me. It doesn't address the issue of 
config.dict() string interpolating the format key, but it does what I want it 
to do and that's my bottom line.

Test script:

import logging, logging.config, logging.handlers
import configobj

logconfig = configobj.ConfigObj('loggingtest.ini', unrepr=True, 
raise_errors=True)

config=logconfig['loggng']
config['handlers']['file']['filename'] = logconfig['loggng']['LogFile']
logging.config.dictConfig(config)
formatters = config['formatters']['fmt1']
logger=logging.getLogger('root')
print(config['handlers']['file'])
for h in logger.handlers:
print('handler {}, stream {}'.format(h.name, h.stream))


loggingtest.ini:
###
[loggng]
version = 1
level = 'INFO'
RootDir = 'TestData'
CaptureDrive = 'C:/'
LogFile = '%(CaptureDrive)s%(RootDir)s/test.log'
[[formatters]]
[[[fmt1]]]
format = '%(asctime)s (%(levelname)s)  %(message)s'
datefmt =
[[handlers]]
[[[console]]]
class = 'logging.StreamHandler'
level = 'INFO'
#stream = 'ext://sys.stdout'
formatter = 'fmt1'
[[[file]]]
class = 'logging.FileHandler'
level = 'WARN'
formatter = 'fmt1'  
[[loggers]]
[[[root]]]
level = 'INFO'
handlers = ['file','console']
###
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: using configobj string interpolation and logging.config.dictConfig

2017-05-26 Thread Tim Williams
On Friday, May 26, 2017 at 8:37:38 AM UTC-4, Tim Williams wrote:
> On Friday, May 26, 2017 at 8:32:19 AM UTC-4, Tim Williams wrote:
> > On Thursday, May 25, 2017 at 9:43:40 PM UTC-4, Tim Williams wrote:
> > > On Thursday, May 25, 2017 at 5:16:13 PM UTC-4, Peter Otten wrote:
> > (snip)
> > > > ...
> > > > 
> > > > How do you get
> > > > 
> > > > > LogFile = '%(CaptureDrive)s%(RootDir)s/test.log'
> > > > 
> > > > to be interpolated while leaving
> > > > 
> > > > > format = '%(asctime)s: (%(levelname)s)  %(message)s'
> > > > 
> > > > as is?
> > > > 
> > > > > However, when I try to call logging.config.dictConfig() on it, the 
> > > > > stream
> > > > > that is opened on creating the logging.FileHandler object is
> > > > > "%(LogFile)s", not "C:/TestData/test.log".
> > > > 
> > > > I don't even get this far:
> > > > 
> > > > >>> c = configobj.ConfigObj("second.ini", unrepr=True)
> > > > >>> c.dict()
> > > > Traceback (most recent call last):
> > > > ...
> > > > configobj.MissingInterpolationOption: missing option "asctime" in 
> > > > interpolation.
> > > > 
> > > > I tried to escape % as %%, but that doesn't seem to work. When I 
> > > > provide 
> > > > bogus replacements
> > > > 
> > > > >>> c = configobj.ConfigObj("third.ini", unrepr=True)
> > > > >>> pprint.pprint(c.dict()["loggng"])
> > > > {'CaptureDrive': 'C:/',
> > > >  'LogFile': 'C:/TestData/test.log',
> > > >  'RootDir': 'TestData',
> > > >  'asctime': 'ASCTIME',
> > > >  'formatters': {'fmt1': {'datefmt': '',
> > > >  'format': 'ASCTIME: (LEVELNAME)  MESSAGE'}},
> > > >  'handlers': {'console': {'class': 'logging.StreamHandler',
> > > >   'level': 'INFO',
> > > >   'stream': 'ext://sys.stdout'},
> > > >   'file': {'class': 'logging.FileHandler',
> > > >'filename': 'C:/TestData/test.log',
> > > >'level': 'WARN'}},
> > > >  'level': 'INFO',
> > > >  'levelname': 'LEVELNAME',
> > > >  'loggers': {'root': {'handlers': ['file', 'console'], 'level': 
> > > > 'INFO'}},
> > > >  'message': 'MESSAGE',
> > > >  'version': 1}
> > > > 
> > > > I get the expected output.
> > > 
> > > I'm at home now, so I don't have my environment, but if I do a c.dict() I 
> > > get the error about asctime also. If I just pass in the dict object or do 
> > > a 'dict(config['loggng'])', I don't get that.
> > 
> > (Back at work.)
> > Looking at the non-interpolation of '%(asctime)s', etc again, I'm wondering 
> > that myself. Maybe this is a bug in configobj? That doesn't make sense 
> > though. I'm wondering if the keyword 'format' has something to do with it.
> 
> Changed key 'foramt' to 'formt'. No change on non-interpolation of 
> '%(asctime)s'

Peter,

I'm starting to think that maybe my problem is somewhere with configobj. 
Calling ConfigObj.dict() (really configobj.Section.dict()) tries to interpolate 
the values in the 'format' key, but not when creating the ConfigObj object, but 
it does interpolate the LogFile key value. 

I've spent too much time trying to track this down. I'll just hard-code my 
filename in my INI file. Maybe I'll get back to it, but I need to move on.

Thanks for your help.
Tim
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: using configobj string interpolation and logging.config.dictConfig

2017-05-26 Thread Tim Williams
On Friday, May 26, 2017 at 8:32:19 AM UTC-4, Tim Williams wrote:
> On Thursday, May 25, 2017 at 9:43:40 PM UTC-4, Tim Williams wrote:
> > On Thursday, May 25, 2017 at 5:16:13 PM UTC-4, Peter Otten wrote:
> (snip)
> > > ...
> > > 
> > > How do you get
> > > 
> > > > LogFile = '%(CaptureDrive)s%(RootDir)s/test.log'
> > > 
> > > to be interpolated while leaving
> > > 
> > > > format = '%(asctime)s: (%(levelname)s)  %(message)s'
> > > 
> > > as is?
> > > 
> > > > However, when I try to call logging.config.dictConfig() on it, the 
> > > > stream
> > > > that is opened on creating the logging.FileHandler object is
> > > > "%(LogFile)s", not "C:/TestData/test.log".
> > > 
> > > I don't even get this far:
> > > 
> > > >>> c = configobj.ConfigObj("second.ini", unrepr=True)
> > > >>> c.dict()
> > > Traceback (most recent call last):
> > > ...
> > > configobj.MissingInterpolationOption: missing option "asctime" in 
> > > interpolation.
> > > 
> > > I tried to escape % as %%, but that doesn't seem to work. When I provide 
> > > bogus replacements
> > > 
> > > >>> c = configobj.ConfigObj("third.ini", unrepr=True)
> > > >>> pprint.pprint(c.dict()["loggng"])
> > > {'CaptureDrive': 'C:/',
> > >  'LogFile': 'C:/TestData/test.log',
> > >  'RootDir': 'TestData',
> > >  'asctime': 'ASCTIME',
> > >  'formatters': {'fmt1': {'datefmt': '',
> > >  'format': 'ASCTIME: (LEVELNAME)  MESSAGE'}},
> > >  'handlers': {'console': {'class': 'logging.StreamHandler',
> > >   'level': 'INFO',
> > >   'stream': 'ext://sys.stdout'},
> > >   'file': {'class': 'logging.FileHandler',
> > >'filename': 'C:/TestData/test.log',
> > >'level': 'WARN'}},
> > >  'level': 'INFO',
> > >  'levelname': 'LEVELNAME',
> > >  'loggers': {'root': {'handlers': ['file', 'console'], 'level': 'INFO'}},
> > >  'message': 'MESSAGE',
> > >  'version': 1}
> > > 
> > > I get the expected output.
> > 
> > I'm at home now, so I don't have my environment, but if I do a c.dict() I 
> > get the error about asctime also. If I just pass in the dict object or do a 
> > 'dict(config['loggng'])', I don't get that.
> 
> (Back at work.)
> Looking at the non-interpolation of '%(asctime)s', etc again, I'm wondering 
> that myself. Maybe this is a bug in configobj? That doesn't make sense 
> though. I'm wondering if the keyword 'format' has something to do with it.

Changed key 'foramt' to 'formt'. No change on non-interpolation of 
'%(asctime)s'
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: using configobj string interpolation and logging.config.dictConfig

2017-05-26 Thread Tim Williams
On Thursday, May 25, 2017 at 9:43:40 PM UTC-4, Tim Williams wrote:
> On Thursday, May 25, 2017 at 5:16:13 PM UTC-4, Peter Otten wrote:
(snip)
> > ...
> > 
> > How do you get
> > 
> > > LogFile = '%(CaptureDrive)s%(RootDir)s/test.log'
> > 
> > to be interpolated while leaving
> > 
> > > format = '%(asctime)s: (%(levelname)s)  %(message)s'
> > 
> > as is?
> > 
> > > However, when I try to call logging.config.dictConfig() on it, the stream
> > > that is opened on creating the logging.FileHandler object is
> > > "%(LogFile)s", not "C:/TestData/test.log".
> > 
> > I don't even get this far:
> > 
> > >>> c = configobj.ConfigObj("second.ini", unrepr=True)
> > >>> c.dict()
> > Traceback (most recent call last):
> > ...
> > configobj.MissingInterpolationOption: missing option "asctime" in 
> > interpolation.
> > 
> > I tried to escape % as %%, but that doesn't seem to work. When I provide 
> > bogus replacements
> > 
> > >>> c = configobj.ConfigObj("third.ini", unrepr=True)
> > >>> pprint.pprint(c.dict()["loggng"])
> > {'CaptureDrive': 'C:/',
> >  'LogFile': 'C:/TestData/test.log',
> >  'RootDir': 'TestData',
> >  'asctime': 'ASCTIME',
> >  'formatters': {'fmt1': {'datefmt': '',
> >  'format': 'ASCTIME: (LEVELNAME)  MESSAGE'}},
> >  'handlers': {'console': {'class': 'logging.StreamHandler',
> >   'level': 'INFO',
> >   'stream': 'ext://sys.stdout'},
> >   'file': {'class': 'logging.FileHandler',
> >'filename': 'C:/TestData/test.log',
> >'level': 'WARN'}},
> >  'level': 'INFO',
> >  'levelname': 'LEVELNAME',
> >  'loggers': {'root': {'handlers': ['file', 'console'], 'level': 'INFO'}},
> >  'message': 'MESSAGE',
> >  'version': 1}
> > 
> > I get the expected output.
> 
> I'm at home now, so I don't have my environment, but if I do a c.dict() I get 
> the error about asctime also. If I just pass in the dict object or do a 
> 'dict(config['loggng'])', I don't get that.

(Back at work.)
Looking at the non-interpolation of '%(asctime)s', etc again, I'm wondering 
that myself. Maybe this is a bug in configobj? That doesn't make sense though. 
I'm wondering if the keyword 'format' has something to do with it.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: using configobj string interpolation and logging.config.dictConfig

2017-05-25 Thread Tim Williams
On Thursday, May 25, 2017 at 5:16:13 PM UTC-4, Peter Otten wrote:
> Tim Williams wrote:
> 
> > On Wednesday, May 24, 2017 at 5:47:37 PM UTC-4, Peter Otten wrote:
> >> Tim Williams wrote:
> >> 
> >> > Just as a followup, if I use 'unrepr=True' in my ConfigObj, I don't
> >> > have to convert the strings.
> >> 
> >> I'd keep it simple and would use JSON...
> > 
> > I looked at JSON at first, but went with configobj because I didn't see
> > where it did string interpolation, which I needed for other parts of my
> > INI file, and I'm trying to use it to specify my log file in my handler.
> > 
> > Which brings me to ...
> 
> > I have this stripped down INI file:
> > 
> ...
> 
> How do you get
> 
> > LogFile = '%(CaptureDrive)s%(RootDir)s/test.log'
> 
> to be interpolated while leaving
> 
> > format = '%(asctime)s: (%(levelname)s)  %(message)s'
> 
> as is?
> 
> > However, when I try to call logging.config.dictConfig() on it, the stream
> > that is opened on creating the logging.FileHandler object is
> > "%(LogFile)s", not "C:/TestData/test.log".
> 
> I don't even get this far:
> 
> >>> c = configobj.ConfigObj("second.ini", unrepr=True)
> >>> c.dict()
> Traceback (most recent call last):
> ...
> configobj.MissingInterpolationOption: missing option "asctime" in 
> interpolation.
> 
> I tried to escape % as %%, but that doesn't seem to work. When I provide 
> bogus replacements
> 
> >>> c = configobj.ConfigObj("third.ini", unrepr=True)
> >>> pprint.pprint(c.dict()["loggng"])
> {'CaptureDrive': 'C:/',
>  'LogFile': 'C:/TestData/test.log',
>  'RootDir': 'TestData',
>  'asctime': 'ASCTIME',
>  'formatters': {'fmt1': {'datefmt': '',
>  'format': 'ASCTIME: (LEVELNAME)  MESSAGE'}},
>  'handlers': {'console': {'class': 'logging.StreamHandler',
>   'level': 'INFO',
>   'stream': 'ext://sys.stdout'},
>   'file': {'class': 'logging.FileHandler',
>'filename': 'C:/TestData/test.log',
>'level': 'WARN'}},
>  'level': 'INFO',
>  'levelname': 'LEVELNAME',
>  'loggers': {'root': {'handlers': ['file', 'console'], 'level': 'INFO'}},
>  'message': 'MESSAGE',
>  'version': 1}
> 
> I get the expected output.

I'm at home now, so I don't have my environment, but if I do a c.dict() I get 
the error about asctime also. If I just pass in the dict object or do a 
'dict(config['loggng'])', I don't get that.
-- 
https://mail.python.org/mailman/listinfo/python-list


using configobj string interpolation and logging.config.dictConfig (was Re: dictConfig: logging.StreamHandler object is not iterable.)

2017-05-25 Thread Tim Williams
On Wednesday, May 24, 2017 at 5:47:37 PM UTC-4, Peter Otten wrote:
> Tim Williams wrote:
> 
> > Just as a followup, if I use 'unrepr=True' in my ConfigObj, I don't have
> > to convert the strings.
> 
> I'd keep it simple and would use JSON...

I looked at JSON at first, but went with configobj because I didn't see where 
it did string interpolation, which I needed for other parts of my INI file, and 
I'm trying to use it to specify my log file in my handler.

Which brings me to ...

I have this stripped down INI file:

[loggng]
version = 1
level = 'INFO'
RootDir = 'TestData'
CaptureDrive = 'C:/'
LogFile = '%(CaptureDrive)s%(RootDir)s/test.log'
[[formatters]]
[[[fmt1]]]
format = '%(asctime)s: (%(levelname)s)  %(message)s'
datefmt =
[[handlers]]
[[[console]]]
class = 'logging.StreamHandler'
level = 'INFO'
stream = 'ext://sys.stdout'
[[[file]]]
class = 'logging.FileHandler'
level = 'WARN'
filename = '%(LogFile)s'
#filename = 'cfg://loggng.LogFile'
[[loggers]]
[[[root]]]
level = 'INFO'
handlers = ['file','console']

When I parse the INI file with configobj:
config = configobj.ConfigObj('loggingtest.ini', unrepr=True, raise_errors=True)

config['loggng']['handlers']['file']['filename'] evaluates correctly to 
C:/TestData/test.log

However, when I try to call logging.config.dictConfig() on it, the stream that 
is opened on creating the logging.FileHandler object is "%(LogFile)s", not 
"C:/TestData/test.log".

Stepping into the debugger, I see that the dictionary is changed in 
BaseConfigurator.convert()

def convert(self, value):
"""
Convert values to an appropriate type. dicts, lists and tuples are
replaced by their converting alternatives. Strings are checked to
see if they have a conversion format and are converted if they do.
"""
if not isinstance(value, ConvertingDict) and isinstance(value, dict):
value = ConvertingDict(value)
value.configurator = self

BEFORE calling ConvertingDict(value), the value dict has the correct value for 
key filename:

>>> value
{'class': 'logging.FileHandler', 'level': 'WARN', 'filename': 
'C:/TestData/test.log'}

AFTER calling ConvertingDict(value):

>>> value
{'filename': '%(LogFile)s', 'class': 'logging.FileHandler', 'level': 'WARN'}

If I try to use 
filename = 'cfg://loggng.LogFile'
in my INI file, I get a ValueError calling logging.config.dictConfig(config):


pydev debugger: starting (pid: 70744)
Traceback (most recent call last):
  File "C:\Python34\lib\logging\config.py", line 557, in configure
handler = self.configure_handler(handlers[name])
  File "C:\Python34\lib\logging\config.py", line 723, in configure_handler
kwargs = dict([(k, config[k]) for k in config if valid_ident(k)])
  File "C:\Python34\lib\logging\config.py", line 723, in 
kwargs = dict([(k, config[k]) for k in config if valid_ident(k)])
  File "C:\Python34\lib\logging\config.py", line 318, in __getitem__
return self.convert_with_key(key, value)
  File "C:\Python34\lib\logging\config.py", line 284, in convert_with_key
result = self.configurator.convert(value)
  File "C:\Python34\lib\logging\config.py", line 455, in convert
value = converter(suffix)
  File "C:\Python34\lib\logging\config.py", line 404, in cfg_convert
d = self.config[m.groups()[0]]
  File "C:\Python34\lib\logging\config.py", line 317, in __getitem__
value = dict.__getitem__(self, key)
KeyError: 'loggng'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "L:\timothy.j.williams1\Documents\My 
Programs\eclipse_neon\eclipse\plugins\org.python.pydev_5.3.0.201610121612\pysrc\pydevd.py",
 line 1531, in 
globals = debugger.run(setup['file'], None, None, is_module)
  File "L:\timothy.j.williams1\Documents\My 
Programs\eclipse_neon\eclipse\plugins\org.python.pydev_5.3.0.201610121612\pysrc\pydevd.py",
 line 938, in run
pydev_imports.execfile(file, globals, locals)  # execute the script
  File "L:\timothy.j.williams1\Documents\My 
Programs\eclipse_neon\eclipse\plugins\org.python.pydev_5.3.0.201610121612\pysrc\_pydev_imps\_pydev_execfile.py",
 line 25, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "L:\workspace\MyPython\src\testlog.py", line 8, in 
logging.config.dictConfig(config)
  File "C:\Python34\lib\logging\config.py", line 789, in dictConfig
 

Re: dictConfig: logging.StreamHandler object is not iterable.

2017-05-24 Thread Tim Williams
On Wednesday, May 24, 2017 at 2:46:54 PM UTC-4, Tim Williams wrote:
> (Apologies for using Google Groups to post)
> 
> I'm trying to use dictConfig to configure logging. I keep running into the 
> error that the logging.StreamHandler object is not iterable.
> 
> I'm using Python 3.4.3 on a Windows 7 box.
> 
> C:\Python34\python.exe 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) 
> [MSC v.1600 64 bit (AMD64)]
> 
> 
> I want to use the configobj module to create the dictionary from an INI file, 
> but that's not the problem I'm having. 
> 
> Here is my test code:
> #
> import logging, logging.config, logging.handlers
> import configobj
> 
> # config = configobj.ConfigObj('loggingtest.ini')
> # config['version']=eval(config['version'])
> config = {
>   'version': 1, 
>   'level': 'INFO',
>   'formatters': {'fmt1': {'format': '%(asctime)s: (%(levelname)s)  
> %(message)s', 
>   'datefmt': ''}
>  }, 
>   'loggers': {'root': {'level': 'INFO', 
>'handlers': 'cfg://handlers.console'}, 
>   'file': {'level': 'WARN', 
>'handlers': 'cfg://handlers.file'}
>   }, 
>   'handlers': {'console': {'class': 'logging.StreamHandler', 
>'level': 'INFO', 
>'stream': 'ext://sys.stdout'}, 
>'file': {'class': 'logging.FileHandler', 
> 'level': 'WARN', 
> 'filename': 'test.log'}
>}, 
>   }
> 
> 
> logging.config.dictConfig(config)
> 
> 
> When I run it, I get this traceback:
> 
> Traceback (most recent call last):
>   File "C:\Python34\lib\logging\config.py", line 611, in configure
> self.configure_logger(name, loggers[name])
>   File "C:\Python34\lib\logging\config.py", line 775, in configure_logger
> self.common_logger_config(logger, config, incremental)
>   File "C:\Python34\lib\logging\config.py", line 767, in common_logger_config
> self.add_handlers(logger, handlers)
>   File "C:\Python34\lib\logging\config.py", line 748, in add_handlers
> for h in handlers:
> TypeError: 'StreamHandler' object is not iterable
> 
> During handling of the above exception, another exception occurred:
> 
> Traceback (most recent call last):
>   File "L:\workspace\MyPython\src\testlog.py", line 27, in 
> logging.config.dictConfig(config)
>   File "C:\Python34\lib\logging\config.py", line 789, in dictConfig
> dictConfigClass(config).configure()
>   File "C:\Python34\lib\logging\config.py", line 614, in configure
> '%r: %s' % (name, e))
> ValueError: Unable to configure logger 'root': 'StreamHandler' object is not 
> iterable
> 
> 
> I even tried creating a JSON file to create the dictionary, and got the same 
> error.
> 
> Thanks for any help
> --
> Tim

Just as a followup, if I use 'unrepr=True' in my ConfigObj, I don't have to 
convert the strings.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: dictConfig: logging.StreamHandler object is not iterable.

2017-05-24 Thread Tim Williams
On Wednesday, May 24, 2017 at 2:46:54 PM UTC-4, Tim Williams wrote:
> (Apologies for using Google Groups to post)
> 
> I'm trying to use dictConfig to configure logging. I keep running into the 
> error that the logging.StreamHandler object is not iterable.
> 
> I'm using Python 3.4.3 on a Windows 7 box.
> 
> C:\Python34\python.exe 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) 
> [MSC v.1600 64 bit (AMD64)]
> 
> 
> I want to use the configobj module to create the dictionary from an INI file, 
> but that's not the problem I'm having. 
> 
> Here is my test code:
> #
> import logging, logging.config, logging.handlers
> import configobj
> 
> # config = configobj.ConfigObj('loggingtest.ini')
> # config['version']=eval(config['version'])
> config = {
>   'version': 1, 
>   'level': 'INFO',
>   'formatters': {'fmt1': {'format': '%(asctime)s: (%(levelname)s)  
> %(message)s', 
>   'datefmt': ''}
>  }, 
>   'loggers': {'root': {'level': 'INFO', 
>'handlers': 'cfg://handlers.console'}, 
>   'file': {'level': 'WARN', 
>'handlers': 'cfg://handlers.file'}
>   }, 
>   'handlers': {'console': {'class': 'logging.StreamHandler', 
>'level': 'INFO', 
>'stream': 'ext://sys.stdout'}, 
>'file': {'class': 'logging.FileHandler', 
> 'level': 'WARN', 
> 'filename': 'test.log'}
>}, 
>   }
> 
> 
> logging.config.dictConfig(config)
> 
> 
> When I run it, I get this traceback:
> 
> Traceback (most recent call last):
>   File "C:\Python34\lib\logging\config.py", line 611, in configure
> self.configure_logger(name, loggers[name])
>   File "C:\Python34\lib\logging\config.py", line 775, in configure_logger
> self.common_logger_config(logger, config, incremental)
>   File "C:\Python34\lib\logging\config.py", line 767, in common_logger_config
> self.add_handlers(logger, handlers)
>   File "C:\Python34\lib\logging\config.py", line 748, in add_handlers
> for h in handlers:
> TypeError: 'StreamHandler' object is not iterable
> 
> During handling of the above exception, another exception occurred:
> 
> Traceback (most recent call last):
>   File "L:\workspace\MyPython\src\testlog.py", line 27, in 
> logging.config.dictConfig(config)
>   File "C:\Python34\lib\logging\config.py", line 789, in dictConfig
> dictConfigClass(config).configure()
>   File "C:\Python34\lib\logging\config.py", line 614, in configure
> '%r: %s' % (name, e))
> ValueError: Unable to configure logger 'root': 'StreamHandler' object is not 
> iterable
> 
> 
> I even tried creating a JSON file to create the dictionary, and got the same 
> error.
> 
> Thanks for any help
> --
> Tim

Peter,

Thanks. That did it for my dictionary.
What I'm really trying to do is use configobj to do this from an INI file. The 
one problem is that all the values are strings, hence the 

config['version']=eval(config['version'])

It looks like if I do something similar with the handlers in the loggers, like:

config['loggers']['root']['handlers']=eval(config['loggers']['root']['handlers'])
config['loggers']['file']['handlers']=eval(config['loggers']['file']['handlers'])

logging.config.dictConfig(dict(config))

seems to work.

I'm using configobj instead of configparser because configobj can nest 
sections. The INI file I'm trying to build has more sections for other things.

version = 1
level = INFO
[formatters]
[[fmt1]]
format = "%(asctime)s: (%(levelname)s)  %(message)s"
datefmt =
[handlers]
[[console]]
class = logging.StreamHandler
level = INFO
stream = ext://sys.stdout
[[file]]
class = logging.FileHandler
level = WARN
filename = test.log
[loggers]
[[root]]
level = INFO
handlers = [console]
[[file]]
level = WARN
handlers = ['file']
propagate = 1

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


dictConfig: logging.StreamHandler object is not iterable.

2017-05-24 Thread Tim Williams
(Apologies for using Google Groups to post)

I'm trying to use dictConfig to configure logging. I keep running into the 
error that the logging.StreamHandler object is not iterable.

I'm using Python 3.4.3 on a Windows 7 box.

C:\Python34\python.exe 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC 
v.1600 64 bit (AMD64)]


I want to use the configobj module to create the dictionary from an INI file, 
but that's not the problem I'm having. 

Here is my test code:
#
import logging, logging.config, logging.handlers
import configobj

# config = configobj.ConfigObj('loggingtest.ini')
# config['version']=eval(config['version'])
config = {
  'version': 1, 
  'level': 'INFO',
  'formatters': {'fmt1': {'format': '%(asctime)s: (%(levelname)s)  
%(message)s', 
  'datefmt': ''}
 }, 
  'loggers': {'root': {'level': 'INFO', 
   'handlers': 'cfg://handlers.console'}, 
  'file': {'level': 'WARN', 
   'handlers': 'cfg://handlers.file'}
  }, 
  'handlers': {'console': {'class': 'logging.StreamHandler', 
   'level': 'INFO', 
   'stream': 'ext://sys.stdout'}, 
   'file': {'class': 'logging.FileHandler', 
'level': 'WARN', 
'filename': 'test.log'}
   }, 
  }


logging.config.dictConfig(config)


When I run it, I get this traceback:

Traceback (most recent call last):
  File "C:\Python34\lib\logging\config.py", line 611, in configure
self.configure_logger(name, loggers[name])
  File "C:\Python34\lib\logging\config.py", line 775, in configure_logger
self.common_logger_config(logger, config, incremental)
  File "C:\Python34\lib\logging\config.py", line 767, in common_logger_config
self.add_handlers(logger, handlers)
  File "C:\Python34\lib\logging\config.py", line 748, in add_handlers
for h in handlers:
TypeError: 'StreamHandler' object is not iterable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "L:\workspace\MyPython\src\testlog.py", line 27, in 
logging.config.dictConfig(config)
  File "C:\Python34\lib\logging\config.py", line 789, in dictConfig
dictConfigClass(config).configure()
  File "C:\Python34\lib\logging\config.py", line 614, in configure
'%r: %s' % (name, e))
ValueError: Unable to configure logger 'root': 'StreamHandler' object is not 
iterable


I even tried creating a JSON file to create the dictionary, and got the same 
error.

Thanks for any help
--
Tim
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: numpy array operation

2013-01-29 Thread Tim Williams
On Tuesday, January 29, 2013 3:41:54 AM UTC-5, C. Ng wrote:
 Is there a numpy operation that does the following to the array?
 
 
 
 1 2  ==  4 3
 
 3 4   2 1
 
 
 
 Thanks in advance.

 import numpy as np
 a=np.array([[1,2],[3,4]])
 a
array([[1, 2],
   [3, 4]])
 np.fliplr(np.flipud(a))
array([[4, 3],
   [2, 1]])

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


Re: Accessing dll

2012-09-06 Thread Tim Williams
On Thursday, September 6, 2012 11:07:07 AM UTC-4, Helpful person wrote:
 I am a complete novice to Python.  I wish to access a dll that has
 
 been written to be compatible with C and VB6.  I have been told that
 
 after running Python I should enter  from ctypes import * which
 
 allows Python to recognize the dll structure.  I have placed the dll
 
 into my active directory (if that's the correct word, one on my path)
 
 for simplification.
 
 
 
 I tried:   import name.dll but this just gave me an error telling me
 
 that there was no such module.
 
 
 
 Can someone please help?
 
 
 
 Richard

I'm new to using the ctypes module also, but what I did to find the library was 
I appended the location of the dll to my PATH like so: (this is Windows)

pth = os.environ['path'].split(';')
pth.append(os.path.join(os.environ['userprofile'],'My Documents','DLLs'))
os.environ['path'] = ';'.join(pth)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Accessing dll

2012-09-06 Thread Tim Williams
On Thursday, September 6, 2012 4:21:56 PM UTC-4, Tim Williams wrote:
 On Thursday, September 6, 2012 11:07:07 AM UTC-4, Helpful person wrote:
 
  I am a complete novice to Python.  I wish to access a dll that has
 
  
 
  been written to be compatible with C and VB6.  I have been told that
 
  
 
  after running Python I should enter  from ctypes import * which
 
  
 
  allows Python to recognize the dll structure.  I have placed the dll
 
  
 
  into my active directory (if that's the correct word, one on my path)
 
  
 
  for simplification.
 
  
 
  
 
  
 
  I tried:   import name.dll but this just gave me an error telling me
 
  
 
  that there was no such module.
 
  
 
  
 
  
 
  Can someone please help?
 
  
 
  
 
  
 
  Richard
 
 
 
 I'm new to using the ctypes module also, but what I did to find the library 
 was I appended the location of the dll to my PATH like so: (this is Windows)
 
 
 
 pth = os.environ['path'].split(';')
 
 pth.append(os.path.join(os.environ['userprofile'],'My Documents','DLLs'))
 
 os.environ['path'] = ';'.join(pth)

I should have also mentioned to look at LoadLibrary in the ctypes module. e.g. 

mylib=cdll.LoadLibrary('mylib.dll')
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: calling loaded DLL function expecting POINT * argument

2012-09-04 Thread Tim Williams
On Sunday, August 26, 2012 9:23:49 PM UTC-4, Tim Roberts wrote:
 Tim Williams tjand...@cox.net wrote:
 
 
 
 Hello all,
 
 
 
 I'm trying to use the ctypes module to call functions in a DLL. I've
 
 figured out how to modify my path so the library is found, and I can 
 
 call LoadLibrary on it, but one of the functions expects an array of
 
  POINTS. Here is the prototype from the .h file:
 
 
 
 
 
 TRACKER_API HRESULT InitializeMask(HANDLE pHandle, int nWidth, int nHeight, 
 POINT* ptMasks, int nNumPoints);
 
 
 
 How is TRACKER_API defined?  You're using ctypes.oledll, which uses the
 
 __stdcall calling convention.  It's possible your DLL is defined as
 
 __cdecl.  Try cdll.LoadLibrary instead of oledll.LoadLibrary.
 
 -- 
 
 Tim Roberts, t...@probo.com
 
 Providenza  Boekelheide, Inc.

Thanks for the reply. I've been out all last week. I'll give it a try.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: calling loaded DLL function expecting POINT * argument

2012-09-04 Thread Tim Williams
On Tuesday, September 4, 2012 8:16:33 AM UTC-4, Tim Williams wrote:
 On Sunday, August 26, 2012 9:23:49 PM UTC-4, Tim Roberts wrote:
 
  Tim Williams tjand...@cox.net wrote:
 
  
 
  
 
  
 
  Hello all,
 
  
 
  
 
  
 
  I'm trying to use the ctypes module to call functions in a DLL. I've
 
  
 
  figured out how to modify my path so the library is found, and I can 
 
  
 
  call LoadLibrary on it, but one of the functions expects an array of
 
  
 
   POINTS. Here is the prototype from the .h file:
 
  
 
  
 
  
 
  
 
  
 
  TRACKER_API HRESULT InitializeMask(HANDLE pHandle, int nWidth, int 
  nHeight, POINT* ptMasks, int nNumPoints);
 
  
 
  
 
  
 
  How is TRACKER_API defined?  You're using ctypes.oledll, which uses the
 
  
 
  __stdcall calling convention.  It's possible your DLL is defined as
 
  
 
  __cdecl.  Try cdll.LoadLibrary instead of oledll.LoadLibrary.
 
  
 
  -- 
 
  
 
  Tim Roberts, t...@probo.com
 
  
 
  Providenza  Boekelheide, Inc.
 
 
 
 Thanks for the reply. I've been out all last week. I'll give it a try.

cdll.LoadLibrary did trick! Thanks again. Now on to the next step
-- 
http://mail.python.org/mailman/listinfo/python-list


calling loaded DLL function expecting POINT * argument

2012-08-24 Thread Tim Williams
Hello all,

I'm trying to use the ctypes module to call functions in a DLL. I've figured 
out how to modify my path so the library is found, and I can call LoadLibrary 
on it, but one of the functions expects an array of POINTS. Here is the 
prototype from the .h file:


TRACKER_API HRESULT InitializeMask(HANDLE pHandle, int nWidth, int nHeight, 
POINT* ptMasks, int nNumPoints);

I did a 

from ctypes.wintypes import *

Here's my test script:
##
'''
Created on Aug 23, 2012

@author: williams
'''
import os
import numpy as np
import scipy.io
from ctypes import *
from ctypes.wintypes import *


matdata = 
scipy.io.loadmat(os.path.join(os.environ['userprofile'],'Desktop','S31_f1.mat'))
data = matdata['data']
nHeight,nWidth = data.shape

pth = os.environ['path'].split(';')
pth.append(os.path.join(os.environ['userprofile'],'My Documents','DLLs'))
os.environ['path'] = ';'.join(pth)
tck=oledll.LoadLibrary('Tracker')
hTrkr = tck.CreateTracker()
maskImage = np.zeros((1024,1280),dtype='int32')
maskImage[300:305,100:105] = True
idx = np.array(maskImage.nonzero())
nPoint = idx.shape[1]

ptMaskType = nPoint * POINT
pts = zip(idx[1,:],idx[0,:])
ptMasks = ptMaskType(*pts)

tck.InitializeMask.argtypes=(HANDLE, c_int, c_int, c_void_p, c_int)
InitMaskResult = tck.InitializeMask(hTrkr, nWidth, nHeight, ptMasks, nPoint)

if __name__ == '__main__':
pass
##

so I have the POINT class, and I've created the array of POINTs. When I try to 
call this function, I get this message:

 InitMaskResult = tck.InitializeMask(hTrkr, nWidth, nHeight, ptMasks, nPoint)
Traceback (most recent call last):
  File console, line 1, in module
ValueError: Procedure probably called with too many arguments (20 bytes in 
excess)


This is the first time that I've tried to use the ctypes module, so any help is 
appreciated.

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


Re: problem loading matlab data with ompc and python

2012-05-24 Thread Tim Williams
On May 23, 5:10 pm, no1 no1.w...@gmail.com wrote:
 Hi, we're investigating transitioning our company from matlab to python. We 
 found OMPC as a MATLAB m-file-to Python translator, but we're encountering a 
 problem using the translated code to import MATLAB data structures into 
 Python. For example, when we save data within MATLAB this way:

 x.a = 5;
 x.b = 6;
 save -v6 test x

 this saves data in test.mat, with MATLAB version 6 compatibility (OMPC says 
 it's not compatible with the latest versions of MATLAB). The code to read in 
 data in MATLAB is just

 load test

 and when we run it through OMPC we get

 load(mstring('test.mat'))

 but when we run it we get the error message

 File ompclib\ompclib_numpy.py, line 1496, in load
 KeyError: [('a', '|O4'), ('b', '|O4')]

 Reading in simpler data (up to arrays) does not have this problem.

 To get other people in the company to transition, we were hoping that the 
 translation process could be done in one step or on the fly. We could read in 
 MATLAB data using I/O functions imported from scipy, but then the transition 
 isn't seamless any more.

 Is there a simple fix to using OMPC? Or a similar alternative that would work 
 better?

 Thanks

Have you tried using loadmat from the scipy.io module?

http://docs.scipy.org/doc/scipy/reference/io.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Accessing the files by last modified time

2012-03-22 Thread Tim Williams
On Mar 22, 7:33 am, Sangeet mrsang...@gmail.com wrote:
 Hi

 I am new to the python programming language.

 I've been trying to write a script that would access the last modified file 
 in one of my directories. I'm using Win XP.

 I saw a similar topic, on the forum before, however the reply using 
 (os.popen) didn't work out for me. I'm not sure whether it was due to syntax 
 errors either.

 Thanks,
 Sangeet

Check out os.stat()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Read time and date from a text file

2010-11-24 Thread Tim Williams
On Nov 24, 7:45 am, huisky hui...@gmail.com wrote:
 Hi,

 As a newbie, I posted my question here again.
 say i have two dics read from a text file by 'split'.

  cstart

 defaultdict(type 'int', {15424: ['Dec', '6', '18:57:40'], 552:
 ['Dec', '7', '09:31:00'], 15500: ['Dec', '6', '20:17:02'], 18863:
 ['Dec', '7', '13:14:47'], 18291: ['Dec', '6', '21:01:17'], 18969:
 ['Dec', '7', '14:28:42'], 18937: ['Dec', '7', '14:21:34']})

  ccompl

 defaultdict(type 'int', {15424: ['Dec', '6', '19:42:55'], 18291:
 ['Dec', '6', '21:01:28'], 15500: ['Dec', '6', '20:26:03'], 18863:
 ['Dec', '7', '13:24:07']})

 and I need to calculate the difference time if the key value is the
 same in both dics.

 Someone suggested me to use the module 'datetime', but I'm still
 wondering how to make it work.
 I mean how to assign ['Dec','6','21:01:17'] to a 'datetime' object and
 then do the datetime operation.

 time=datetime.datetime(cstart[18291])       does NOT work.

 thanks in advance
 Huisky

You can use datetime.datetime.strptime() to create a datetime object
from a string representing a date


 import datetime
 datetime.datetime.strptime('Dec 7  13:24:07','%b %d %H:%M:%S')
datetime.datetime(1900, 12, 7, 13, 24, 7)

Of course, you need to put in the correct year.

datetime.strptime(date_string, format)
Return a datetime corresponding to date_string, parsed according to
format. This is equivalent to datetime(*(time.strptime(date_string,
format)[0:6])). ValueError is raised if the date_string and format
can’t be parsed by time.strptime() or if it returns a value which
isn’t a time tuple.

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


Re: smtplib problem, Unable to relay for

2007-12-21 Thread Tim Williams
On 21/12/2007, Benedict Verheyen [EMAIL PROTECTED] wrote:
 Hi,

 i get an Unable to relay for when trying to send an email from within
 my network to an email address not on my domain.
 I don't understand why it says relaying as i'm sending from an
 internal domain user to an external user.
 Email server is exchange 2003

 See this trace

   smtp.sendmail([EMAIL PROTECTED], [[EMAIL PROTECTED]], msg)
 Traceback (most recent call last):
   File stdin, line 1, in module
   File C:\Python25\Lib\smtplib.py, line 695, in sendmail
 raise SMTPRecipientsRefused(senderrs)
 smtplib.SMTPRecipientsRefused: {'[EMAIL PROTECTED]': (550, '5.7.1
 Unable to relay for [EMAIL PROTECTED]')}

 The smpt var is this:
 smtp = smtplib.SMTP(server) where server is my mail server

 I can mail to any user that is part of mydomain.be but as soon as i try
 an external address, i get the Unable to relay message.

 With a mail program it works but i need to be able to do it via code.
 I thought that i would have to authenticate (smtp.login) to our mail
 server but that didn't resolve the issue

 Any idea how i could do this?

Have you asked your Exchange admin if the server is allowed to relay via SMTP?

The Exchange server is returning a valid response so it's most likely
that it is just not configured to relay (via SMTP).

If your working mail program is Outlook then it is using MAPI not
SMTP to send/receive email.

If your working mail program is an SMTP email client, or you have
added a POP3/IMAP  SMTP account to your Outlook, then something
strange is happening and relaying is probably allowed. You would need
to talk to your exchange admin to see what's shown in the server logs
for your failed SMTPLIB attempts.

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


Re: Pulling data from a .asps site

2007-11-27 Thread Tim Williams
On 27/11/2007, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
 There's a government website which shows public data for banks. We'd
 like to pull the data down programmatically but the data is hidden
 behind .aspx...

 Is there anyway in Python to hook in directly to a browser (firefox or
 IE) to do the following...

 1) Fill the search criteria
 2) Press the Search button
 3) Press another button (the CSV button) on the resulting page
 4) Then grab the data out of the notepad file that pops up

 If this is a wild good chase, let me know... (or if there's a better
 way besides Python... I may have to explore writing a firefox plug-in
 or something)...

Amongst others, IEC will allow you to open an IE browser window,
navigate around sites (including pushing buttons), and retrieve text.

http://www.mayukhbose.com/python/IEC/index.php

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


Re: local variable referenced before assignment

2007-10-25 Thread Tim Williams
On 25/10/2007, A.T.Hofkamp [EMAIL PROTECTED] wrote:
 On 2007-10-25, Pete Bartonly [EMAIL PROTECTED] wrote:
 
 Also, brackets around conditions (in the if) are not needed, and comparing
 against None is usually done with 'is' or 'is not' instead of '==' or '!='.
 The result is then

 if logMsg is not None:

Or just

  if logMsg:
do_something()

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


Re: test if email

2007-10-12 Thread Tim Williams
On 12/10/2007, brad [EMAIL PROTECTED] wrote:
 Florian Lindner wrote:
  Hello,
  is there a function in the Python stdlib to test if a string is a valid
  email address?

 Nope, most any string with an @ in it could be a valid email addy. Send
 a message to the addy, if it doesn't bounce, then it's valid.

Invalid email doesn't always bounce, ( or the bounces may not always reach you )

:)

-- 

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


Re: test if email

2007-10-12 Thread Tim Williams
On 12/10/2007, Florian Lindner [EMAIL PROTECTED] wrote:
 Hello,
 is there a function in the Python stdlib to test if a string is a valid
 email address?


You mean a valid SMTP email address?

In reality, there isn't a way of doing this.  But a good rule of thumb
is if it hasn't got at least one '@' symbol with at least one '.' to
the right of the right-most '@' symbol then it isn't valid for
internet transmission.

If you meant test if a string is an existing email address,  then
again there is no foolproof way of testing.   Valid email addresses
can bounce for a variety of reasons, and invalid email addresses don't
always bounce.  Using an SMTP dialogue with the server without sending
an email can have the same bounce/non-bounce (actually, accept/refuse)
results.

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


Re: Finding Peoples' Names in Files

2007-10-11 Thread Tim Williams
On 11/10/2007, brad [EMAIL PROTECTED] wrote:
 Crazy question, but has anyone attempted this or seen Python code that
 does? For example, if a text file contained 'Guido' and or 'Robert' and
 or 'Susan', then we should return True, otherwise return False.
 --
 http://mail.python.org/mailman/listinfo/python-list



Text = open(fname).read()

def a_function():
for Name in ['Guido', Robert',Susan']:
   if Name in Text:
   return 1

if a_function():
print A name was found

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


Re: Editing particular lines of a text file.

2007-10-09 Thread Tim Williams
On 09/10/2007, Shriphani [EMAIL PROTECTED] wrote:
 Hello all,

 I am trying to create a script that looks at specific strings in a
 file like:

 msgid I am a disco dancer.

 and compares the part in quotes to the keys in a dictionary, finds the
 value and creates a new line right after this string in the file. I
 have planned to write this as follows:

 1. Open the file in read mode
 2. parse each line to figure out which line contains msgid and use
 the shlex module's split method to go and split this line and pick the
 2nd element list[1].
 3. find the value from the dictionary corresponding to the above
 element.
 4. Insert the line. This part is where I face a problem. How do I
 plainly edit just one line. I would also like to look at some sample
 code that does this.
 5. open a new file and write the new file with the inserted strings to
 it.
 6. close both files opened.


infile = open('infile.txt')
outfile = open('outfile.txt','w')
for line in infile:
if 'msgid' in line:
 #  transform line
 #  make sure the line ending is intact
outfile.write(line)
infile.close()
outfile.close()

or maybe

infile = open('infile.txt')
outfile = open('outfile.txt','w')
new_file = []
for line in infile:
if 'msgid' in line:
 #  transform line
 #  make sure the line ending is intact
new_file.append(line)
outfile.write(''.join(new_file)
infile.close()
outfile.close()




-- 

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


Re: Putting a line from a text file into a variable, then moving to next line

2007-10-07 Thread Tim Williams
On 07/10/2007, Tim Chase [EMAIL PROTECTED] wrote:
  I'm not really sure how readline() works. Is there a way to iterate
  through a file with multiple lines and then putting each line in a
  variable in a loop?

 You can use readlines() to get the whole line (including the
 newline):

  lines = file('x.txt').readlines()

 or you can iterate over the file building a list without the newline:

  lines = [line.rstrip('\n') for line in file('x.txt')]

 Thus, line[0] will be the first line in your file, line[1] will
 be the second, etc.


or splitlines()
  lines = open('x.txt').read().splitlines()

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


Re: recipient validation with smtplib

2007-09-28 Thread Tim Williams
On 28/09/2007, Robin Becker [EMAIL PROTECTED] wrote:
 Tim Williams wrote:
  On 28/09/2007, Robin Becker [EMAIL PROTECTED] wrote:
  Is there a way to use smtplib to get recipient validation. I can use 
  smtplib
  quite happily to send emails using the locahost's sendmail, but sendmail 
  is just
   fire and forget, so some bad addresses eg [EMAIL PROTECTED] don't cause 
  any
  error in the sending application. I know some smtp setups do enforce 
  recipient
  validation, but it doesn't seem terribly easy to do this with sendmail. I
  wondered if there were some way to do this in python?
 
  There is no way of  validating *every* email address you send to using
  SMTP alone.   Some servers accept every address and bounce later - as
  you have found out. So for the purpose of the SMTP client, or relaying
  server,  the address is valid at sending time to those servers.
 
  Checking DNS for MX records is a possibility for removing some bad
  addresses, but it's not fool proof as the RFCs don't require MX
  records to exist for a domain to be able to receive email.  If no MX
  records are present, one (and only one!) IP address returned from the
  domain's A record(s) should be tried.
 
  HTH :)
 Thanks, it's at least ammunition for me to say it cannot easily be done. I 
 found
 this milter

 http://www.jmaimon.com/sendmail/callahead-milter/

 but I think this will cause every send to be checked which is probably not 
 what
 we need.


Hmm,  call-ahead functions just initiate the first part of the SMTP
dialogue - to the RCPT TO,   unless you cache the results so that the
call-ahead only checks each address once a day or so there is no
benefit.  Without caching it will just slow down sending process as
you will get 1.5 SMTP conversations per outgoing message instead of
just 1.However,  they still won't catch invalid email addresses
when the server accepts all addresses* and bounces later.

* all addresses = all addresses on domains local to that server.

I have written functions like this in the past for outbound/inbound
(recipient/sender) address checking,  using combinations of SMTP
dialogue, DNS and port checking, bounce-collection, SPF and other
techniques over the years.   There is no guaranteed way of checking
that all email addresses in your list are either VALID or INVALID.
Valid email addresses can get refused/bounce for a variety of reasons,
and invalid addresses sometimes get accepted/don't bounce at all.

You should work on either a best-effort basis,  running some checks
but accepting that its not foolproof  -OR- use no checks at all
knowing that most invalid email addresses will be handled correctly by
the SMTP processes

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


Re: recipient validation with smtplib

2007-09-28 Thread Tim Williams
On 28/09/2007, Robin Becker [EMAIL PROTECTED] wrote:
 Is there a way to use smtplib to get recipient validation. I can use smtplib
 quite happily to send emails using the locahost's sendmail, but sendmail is 
 just
  fire and forget, so some bad addresses eg [EMAIL PROTECTED] don't cause any
 error in the sending application. I know some smtp setups do enforce recipient
 validation, but it doesn't seem terribly easy to do this with sendmail. I
 wondered if there were some way to do this in python?

There is no way of  validating *every* email address you send to using
SMTP alone.   Some servers accept every address and bounce later - as
you have found out. So for the purpose of the SMTP client, or relaying
server,  the address is valid at sending time to those servers.

Checking DNS for MX records is a possibility for removing some bad
addresses, but it's not fool proof as the RFCs don't require MX
records to exist for a domain to be able to receive email.  If no MX
records are present, one (and only one!) IP address returned from the
domain's A record(s) should be tried.

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


Re: List search

2007-09-28 Thread Tim Williams
On 28/09/2007, Kevin Walzer [EMAIL PROTECTED] wrote:
 I'm having a problem with searching a list. Here's my code:

 mylist = ['x11', 'x11-wm', 'x11-system']

 for line in mylist:
if 'x11' in line:
print line

 This results in the following output:

 x11
 x11-wm
 x11-system


That output is correct,  you are asking your script to print any list
item containing x11 when what you actually wanted was a list item that
is the string 'x11'

mylist = ['x11', 'x11-wm', 'x11-system']
for item in mylist:
  if item ==  'x11':
   print line

If there is only ever one 'x11' in the list you could also consider

print mylist.index('x11')

and

print mylist[mylist.index('x11')]

Also, before iterating the whole list check that 'x11' exists

if 'x11' in mylist:
do stuff

and list comprehesions

print [x for x in mylist if x == 'x11']

HTH :)

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


Re: I could use some help making this Python code run faster using only Python code.

2007-09-20 Thread Tim Williams
On 21/09/2007, Tim Williams [EMAIL PROTECTED] wrote:
 On 20/09/2007, Python Maniac [EMAIL PROTECTED] wrote:
  I am new to Python however I would like some feedback from those who
  know more about Python than I do at this time.
 
  def scrambleLine(line):
 s = ''
 for c in line:
 s += chr(ord(c) | 0x80)
 return s
 
  def descrambleLine(line):
 s = ''
 for c in line:
 s += chr(ord(c)  0x7f)
 return ''.join( [chr(ord(c)  0x7f) for c in line] )
 
  def scrambleFile(fname,action=1):
 if (path.exists(fname)):
 try:
 f = open(fname, r)
 toks = fname.split('.')
 while (len(toks)  2):
 toks.pop()
 fname = '.'.join(toks)
 if (action == 1):
 _fname = fname + '.scrambled'
 elif (action == 0):
 _fname = fname + '.descrambled'
 if (path.exists(_fname)):
 os.remove(_fname)
 ff = open(_fname, w+)
 if (action == 1):
 for l in f:
 ff.write(scrambleLine(l))
 elif (action == 0):
 for l in f:
 ff.write(descrambleLine(l))
 except Exception, details:
 print 'ERROR :: (%s)' % details
 finally:
 f.close()
 ff.close()
 else:
 print 'WARNING :: Missing file %s - cannot continue.' % fname
 
  --


 def scrambleLine(line):
   return ''.join( [chr(ord(c) | 0x80) for c in line] )

 def descrambleLine(line):
return ''.join( [chr(ord(c)  0x7f) for c in line] )

 def scrambleFile(fname,action=1):
   try:
   f = open(fname, r)
   fname = '.'.join(fname.split('.')[:2])
   if action:
   _fname = fname + '.scrambled'
   else:
   _fname = fname + '.descrambled'
   ff = open(_fname, w)
   if action:
   ff.write('\r\n.join([scrambleLine(l) for l in f ]))
   else :
   ff.write('\r\n.join([descrambleLine(l) for l in f ]))
   f.close()
   ff.close()
   except Exception, details:
   print 'ERROR :: (%s)' % details

 HTH :)


or maybe even this:

Apologies for the self-reply, its late here !
(a couple of typos fixed too! )

def scrambleFile(fname,action=1):
  try:
  f = open(fname, r)
  fname = '.'.join(fname.split('.')[:2])
  if action:
  ff = open(fname + '.scrambled', w)
  ff.write('\r\n'.join([scrambleLine(l) for l in f ]))
  else :
  ff = open(fname + '.descrambled', w)
  ff.write('\r\n'.join([descrambleLine(l) for l in f ]))
  f.close()
  ff.close()

  except Exception, details:
  print 'ERROR :: (%s)' % details

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


Re: I could use some help making this Python code run faster using only Python code.

2007-09-20 Thread Tim Williams
On 20/09/2007, Python Maniac [EMAIL PROTECTED] wrote:
 I am new to Python however I would like some feedback from those who
 know more about Python than I do at this time.

 def scrambleLine(line):
s = ''
for c in line:
s += chr(ord(c) | 0x80)
return s

 def descrambleLine(line):
s = ''
for c in line:
s += chr(ord(c)  0x7f)
return ''.join( [chr(ord(c)  0x7f) for c in line] )

 def scrambleFile(fname,action=1):
if (path.exists(fname)):
try:
f = open(fname, r)
toks = fname.split('.')
while (len(toks)  2):
toks.pop()
fname = '.'.join(toks)
if (action == 1):
_fname = fname + '.scrambled'
elif (action == 0):
_fname = fname + '.descrambled'
if (path.exists(_fname)):
os.remove(_fname)
ff = open(_fname, w+)
if (action == 1):
for l in f:
ff.write(scrambleLine(l))
elif (action == 0):
for l in f:
ff.write(descrambleLine(l))
except Exception, details:
print 'ERROR :: (%s)' % details
finally:
f.close()
ff.close()
else:
print 'WARNING :: Missing file %s - cannot continue.' % fname

 --


def scrambleLine(line):
   return ''.join( [chr(ord(c) | 0x80) for c in line] )

def descrambleLine(line):
return ''.join( [chr(ord(c)  0x7f) for c in line] )

def scrambleFile(fname,action=1):
   try:
   f = open(fname, r)
   fname = '.'.join(fname.split('.')[:2])
   if action:
   _fname = fname + '.scrambled'
   else:
   _fname = fname + '.descrambled'
   ff = open(_fname, w)
   if action:
   ff.write('\r\n.join([scrambleLine(l) for l in f ]))
   else :
   ff.write('\r\n.join([descrambleLine(l) for l in f ]))
   f.close()
   ff.close()
   except Exception, details:
   print 'ERROR :: (%s)' % details

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


Re: trim start and trailing space chars from string

2007-09-15 Thread Tim Williams
On 15/09/2007, Konstantinos Pachopoulos [EMAIL PROTECTED] wrote:
 Hi,
 is there something corresponding to the java String.trim() method, ie
 trim start and trailing space/tab chars from string?
 say convert  asdf to asdf?

 ' asdf '.strip()
'asdf'
 ' asdf '.rstrip()
' asdf'
 ' asdf '.lstrip()
'asdf '

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


Re: smtp debugging methods

2007-09-14 Thread Tim Williams
On 14/09/2007, Sean Nakasone [EMAIL PROTECTED] wrote:
 I'm having trouble with sending smtp mail.  It's hanging after the
 smtplib.SMTP() line. It doesn't works from home but not from work.  What's
 the best way to debug this?

 # Here's my script
 import smtplib
 msg = Subject: Hello\n\nThis is the\nbody of the message.
 server = smtplib.SMTP(smtp.gmail.com,465)
   # Connection refused normally means there is no server listening for
   # connections on the specified IP/port-combination.
   # use netstat -an to view connections.
 server.set_debuglevel(1)
 server.ehlo()
 server.starttls()
 server.ehlo()
 # !!! set the password
 server.login(myuser, mypass)
 server.sendmail(...

 # Here's the error
  server = smtplib.SMTP(smtp.gmail.com,465)
 Traceback (most recent call last):
   File stdin, line 1, in ?
   File /usr/lib/python2.4/smtplib.py, line 241, in __init__
 (code, msg) = self.connect(host, port)
   File /usr/lib/python2.4/smtplib.py, line 304, in connect
 (code, msg) = self.getreply()
   File /usr/lib/python2.4/smtplib.py, line 345, in getreply
 line = self.file.readline()
   File /usr/lib/python2.4/socket.py, line 340, in readline
 data = self._sock.recv(self._rbufsize)
 socket.error: (113, 'Software caused connection abort')

There is no SMTP service on port 465 , its some other service or the
smtp server is in trouble,   try it with port 587 instead.

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


Re: extract text from log file using re

2007-09-13 Thread Tim Williams
On 13/09/2007, Fabian Braennstroem [EMAIL PROTECTED] wrote:
 me again... I should describe it better:
 the result should be an array with just:

 498 1.0086e-03 2.4608e-04 9.8589e-05 1.4908e-04 8.3956e-04
 3.8560e-03 4.8384e-02 11:40:01  499
 499 1.0086e-03 2.4608e-04 9.8589e-05 1.4908e-04  8.3956e-04
 3.8560e-03 4.8384e-02 11:40:01  499
 500 1.0049e-03 2.4630e-04 9.8395e-05 1.4865e-04 8.3913e-04
 3.8545e-03 1.3315e-01 11:14:10  500
 501 1.0086e-03 2.4608e-04 9.8589e-05 1.4908e-04 8.3956e-04
 3.8560e-03 4.8384e-02 11:40:01  499

 as field values.

 Fabian Braennstroem schrieb am 09/13/2007 09:09 PM:
  Hi,
 
  I would like to delete a region on a log file which has this
  kind of structure:
 
 
  #--flutest
 498 1.0086e-03 2.4608e-04 9.8589e-05 1.4908e-04
  8.3956e-04 3.8560e-03 4.8384e-02 11:40:01  499
 499 1.0086e-03 2.4608e-04 9.8589e-05 1.4908e-04
  8.3956e-04 3.8560e-03 4.8384e-02 11:40:01  499
  reversed flow in 1 faces on pressure-outlet 35.
 
  Writing
  /home/gcae504/SCR1/Solververgleich/Klimakruemmer_AK/CAD/Daimler/fluent-0500.cas...
   5429199 mixed cells, zone 29, binary.
  11187656 mixed interior faces, zone 30, binary.
 20004 triangular wall faces, zone 31, binary.
  1104 mixed velocity-inlet faces, zone 32, binary.
133638 triangular wall faces, zone 33, binary.
 14529 triangular wall faces, zone 34, binary.
  1350 mixed pressure-outlet faces, zone 35, binary.
 11714 mixed wall faces, zone 36, binary.
   1232141 nodes, binary.
   1232141 node flags, binary.
  Done.
 
 
  Writing
  /home/gcae504/SCR1/Solververgleich/Klimakruemmer_AK/CAD/Daimler/fluent-0500.dat...
  Done.
 
 500 1.0049e-03 2.4630e-04 9.8395e-05 1.4865e-04
  8.3913e-04 3.8545e-03 1.3315e-01 11:14:10  500
 
   reversed flow in 2 faces on pressure-outlet 35.
 501 1.0086e-03 2.4608e-04 9.8589e-05 1.4908e-04
  8.3956e-04 3.8560e-03 4.8384e-02 11:40:01  499
 
  #--
 
  I have a small script, which removes lines starting with
  '(re)versed', '(i)teration' and '(t)urbulent'  and put the
  rest into an array:
 
  # -- plot residuals 
import re
  filename=flutest
  reversed_flow=re.compile('^\ re')
  turbulent_viscosity_ratio=re.compile('^\ tu')
  iteration=re.compile('^\ \ i')
 
  begin_of_res=re.compile('\ \ \ i')
  end_of_res=re.compile('^\ ad')
 
  begin_of_writing=re.compile('^\Writing')
  end_of_writing=re.compile('^\Done')
 
  end_number=0
  begin_number=0
 
 
  n = 0
  for line in open(filename).readlines():
  n = n + 1
  if begin_of_res.match(line):
  begin_number=n+1
  print Line Number (begin):  + str(n)
 
  if end_of_res.match(line):
  end_number=n
  print Line Number (end):  + str(n)
 
  if begin_of_writing.match(line):
  begin_w=n+1
  print BeginWriting:  + str(n)
  print HALLO
 
  if end_of_writing.match(line):
  end_w=n+1
  print EndWriting:  +str(n)
 
  if n  end_number:
  end_number=n
  print Line Number (end):  + str(end_number)
 
 
 
 
 
  n = 0
  array = []
  array_dummy = []
  array_mapped = []
 
  mapped = []
  mappe = []
 
  n = 0
  for line in open(filename).readlines():
  n = n + 1
  if (begin_number = n) and (end_number  n):
  #if (begin_w = n) and (end_w  n):
  if not reversed_flow.match(line) and not
  iteration.match(line) and not
  turbulent_viscosity_ratio.match(line):
  m=(line.strip().split())
  print m
  if len(m)  0:
  #print len(m)
  laenge_liste=len(m)
  #print len(m)
  mappe.append(m)
 
 
  #--end plot
  residuals-
 
  This works fine ; except for the region with the writing
  information:
 
  #-writing information
  -
  Writing /home/fb/fluent-0500.cas...
   5429199 mixed cells, zone 29, binary.
  11187656 mixed interior faces, zone 30, binary.
 20004 triangular wall faces, zone 31, binary.
  1104 mixed velocity-inlet faces, zone 32, binary.
133638 triangular wall faces, zone 33, binary.
 14529 triangular wall faces, zone 34, binary.
  1350 mixed pressure-outlet faces, zone 35, binary.
 11714 mixed wall faces, zone 36, binary.
   1232141 nodes, binary.
   1232141 node flags, binary.
  Done.
  # ---end writing information ---
 
  Does anyone know, how I can this 'writing' stuff too? The
  matchingIt occurs a lot :-(
 

 the result should be an array with just:


 498 1.0086e-03 2.4608e-04 9.8589e-05 1.4908e-04 8.3956e-04
 3.8560e-03 4.8384e-02 11:40:01  499
 499 1.0086e-03 2.4608e-04 9.8589e-05 1.4908e-04  8.3956e-04
 3.8560e-03 4.8384e-02 11:40:01  499
 500 1.0049e-03 2.4630e-04 9.8395e-05 

Re: why should I learn python

2007-09-07 Thread Tim Williams
On 07/09/07, Tom Brown [EMAIL PROTECTED] wrote:
 On Thursday 06 September 2007 16:01, windandwaves wrote:
  Hmmm, thank you all for your replies.  I will do some research on the
  net (i did some already, but because I am really not much of a
  programmer, it is often too detailed for me).  I have limited time,
  but it does sound like something to learn, just for fun and for
  practical use.  How would you use it in a web development
  environment?  I mean, in real practical terms.  Could it assist with
  php? Is it easy to write GUI programs in Python?

 Checkout http://www.djangoproject.com/ or http://turbogears.org/ for web
 development. Checkout http://www.riverbankcomputing.co.uk/pyqt/index.php for
 writing GUI programs. There are other options for GUI apps. That is the one I
 use all the time.

Also, check out Karrigell ( www.karrigell.com) .  It will show you
just how easy  fast Python can be for web development.  It has its
own web server, or it can sit behind Apache etc.

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


Re: startswith( prefix[, start[, end]]) Query

2007-09-06 Thread Tim Williams
On 06/09/07, Bruno Desthuilliers
[EMAIL PROTECTED] wrote:

 You may want to try with a regexp, but I'm not sure it's worth it (hint:
 the timeit module is great for quick small benchmarks).

 Else, you could as well write your own testing function:

 def str_starts_with(astring, *prefixes):
   startswith = astring.startswith
   for prefix in prefixes:
 if startswith(prefix):
   return true
   return false

 for line in f:
   if str_starts_with(line, 'abc, 'de', 'xxx'):
 # CODE HERE


Isn't slicing still faster than startswith?As you mention timeit,
then you should probably add slicing to the pot too :)

if astring[:len(prefix)] == prefix:
 do_stuff()

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


Re: Image.open( C:\test.jpg) is this wrong ?

2007-08-27 Thread Tim Williams
On 27/08/07, Simon Brunning [EMAIL PROTECTED] wrote:
 On 8/27/07, Carnell, James E [EMAIL PROTECTED] wrote:

  Image.open(C:\test.jpg)

 Try:

 Image.open(rC:\test.jpg)

 See http://docs.python.org/ref/strings.html

rC:\test.jpg

also

C:\\test.jpg  or  'C:/test.jpg'
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using Regular Expresions to change .htm to .php in files

2007-08-24 Thread Tim Williams
On 24/08/07, J. Cliff Dyer [EMAIL PROTECTED] wrote:
 Tim Williams wrote:
  On 23/08/07, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
 
  Hi,
 
  I have a bunch of files that have changed from standard htm files to
  php files but all the links inside the site are now broken because
  they point to the .htm files while they are now .php files.
 
  Does anyone have an idea about how to do a simple script that changes
  each .htm in a given file to a .php
 
  Thanks a lot in advance
 
 
 
  Something like:
 
  Infile = open(f_name,'r+')
  Data =  Infile.read()
  InFile.write(Data.replace('.htm','.php'))
  Infile.close()
 
  :)
 
 Yeah, but you'd better make darn sure that *all* links point to .htm
 files (including external links), because you could very easily end up
 pointing to http://some.othersite.com/index.phpl

 And that's just no good.




-- 

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


Re: Using Regular Expresions to change .htm to .php in files

2007-08-23 Thread Tim Williams
On 23/08/07, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
 Hi,

 I have a bunch of files that have changed from standard htm files to
 php files but all the links inside the site are now broken because
 they point to the .htm files while they are now .php files.

 Does anyone have an idea about how to do a simple script that changes
 each .htm in a given file to a .php

 Thanks a lot in advance


Something like:

Infile = open(f_name,'r+')
Data =  Infile.read()
InFile.write(Data.replace('.htm','.php'))
Infile.close()

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


Re: Using Regular Expresions to change .htm to .php in files

2007-08-23 Thread Tim Williams
On 23/08/07, Tim Williams [EMAIL PROTECTED] wrote:
 On 23/08/07, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
  Hi,
 
  I have a bunch of files that have changed from standard htm files to
  php files but all the links inside the site are now broken because
  they point to the .htm files while they are now .php files.
 
  Does anyone have an idea about how to do a simple script that changes
  each .htm in a given file to a .php
 
  Thanks a lot in advance
 

 Something like:

 Infile = open(f_name,'r+')
 Data =  Infile.read()
 InFile.write(Data.replace('.htm','.php'))
 Infile.close()


Actually,  scrub that, its been a long day!You either need a
Infile.seek(0)  in the above before the write() , or you can do.

Data = open(f_name).read()
outfile = open(f_name,'w')
outfile.write(Data.replace('.htm','.php'))
outfile.close()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: reading a line in file

2007-08-20 Thread Tim Williams
On 20/08/07, Brian McCann [EMAIL PROTECTED] wrote:

 Hi,

 I can read in the whole file build.number which has the following lines
 how do I just capture the value of build.number and assign it to a variable
 Thanks,
 Brian

 contents of file build.number:
 #Build Number for ANT. Do not edit!
 #Mon Aug 20 04:04:51 EDT 2007
 build.number=1
 buildinfo.py
 ##
 #!/usr/bin/python
 import string
 import os
 import sys
 import time
 import errno
 import shutil
 buildinfo = build.number
 input = open(buildinfo, 'r')
 for line in input:
 print line
 #

 command line when script is run
 $ buildinfo.py
 #Build Number for ANT. Do not edit!

 #Mon Aug 20 04:04:51 EDT 2007
 build.number=1



This is preferred nowadays

for line in open(buildinfo):
print line

Without a sample of the data, no-one can help you further.   If this
is homework, you should say so, people will give you guidance :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: reading a line in file

2007-08-20 Thread Tim Williams
On 20/08/07, Brian McCann [EMAIL PROTECTED] wrote:



 
 From: [EMAIL PROTECTED] on behalf of Tim Williams
 Sent: Mon 8/20/2007 2:59 PM
 To: Brian McCann
 Cc: python-list@python.org
 Subject: Re: reading a line in file




 On 20/08/07, Brian McCann [EMAIL PROTECTED] wrote:
 
  Hi,
 
  I can read in the whole file build.number which has the following lines
  how do I just capture the value of build.number and assign it to a
 variable
  Thanks,
  Brian
 
  contents of file build.number:
  #Build Number for ANT. Do not edit!
  #Mon Aug 20 04:04:51 EDT 2007
  build.number=1
  buildinfo.py
  ##
  #!/usr/bin/python
  import string
  import os
  import sys
  import time
  import errno
  import shutil
  buildinfo = build.number
  input = open(buildinfo, 'r')
  for line in input:
  print line
  #
 
  command line when script is run
  $ buildinfo.py
  #Build Number for ANT. Do not edit!
 
  #Mon Aug 20 04:04:51 EDT 2007
  build.number=1
 
 

 This is preferred nowadays

 for line in open(buildinfo):
 print line

 Without a sample of the data, no-one can help you further.   If this
 is homework, you should say so, people will give you guidance :)




 Hi Tim,
 The sample data is in file build.number

 contents of file build.number:

  #Build Number for ANT. Do not edit!
  #Mon Aug 20 04:04:51 EDT 2007
 build.number=1

 Thanks,
 Brian

(bottom posting is preferred here :)  )

Brian,  can you expand on this a bit?A single entry doesn't give
much to go on.   20-ish lines would be better.Anyone looking at
this would need to see whether each entry was on its own line, whether
there are duplicates,  whether delimiters are always the same etc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: reading a line in file

2007-08-20 Thread Tim Williams
On 20/08/07, Shawn Milochik [EMAIL PROTECTED] wrote:
 Hopefully this will help (using your input file)

 #!/usr/bin/env python
 import re
 buildinfo = input.txt
 input = open(buildinfo, 'r')

 regex = re.compile(r^\s*build.number=(\d+)\s*$)

 for line in input:
if re.search(regex, line):
print line
buildNum = re.sub(r^\s*build.number=(\d+)\s*$, \\1, line)
print line
print buildNum
 --

If I misunderstood the original post and the build number is all that
is required from the file, then:

buildNum =  open('input.txt').read().split('build.number=')[1].split()[0]

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


Re: reading a line in file

2007-08-20 Thread Tim Williams
On 20/08/07, Brian McCann [EMAIL PROTECTED] wrote:

 Shawn, Tim ,Jay

 many thanks,

 It looks like there are many ways this problem can be approached

 either by using  regex or a tokens

 Tim I'm not familiar with your solution, but will learn about that method
 also

Hi Brian,

 buildNum =  open('input.txt').read().split('build.number=')[1].split()[0]

Try this on its own:

print open('input.txt').read().split('build.number=')

See what is at the very beginning of the 2nd list item :)

My example just splits the whole file at build.number=, so you know
the data you require is at the beginning of the 2nd list item returned
from the slice [ie: a_list[1].  You also know that data is followed by
a newline, so it will be the first item in the returned list [ie:
b_list[0] when you split a_list[1].

No need to iterate each line individually looking for a match and then
process the matching line to get at the data.  You can just go
straight to the data you require.

If your requirement is for multiple items from a single file then
another method would be required.

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


Re: clarification

2007-08-18 Thread Tim Williams
On 17/08/07, Beema shafreen [EMAIL PROTECTED] wrote:
 hi everybody,
 i have a file with data separated by tab
 mydata:
 fhl1fkh2
 dfp1chk1
 mal3alp14
 mal3moe1
 mal3spi1
 mal3bub1
 mal3bub3
 mal3mph1
 mal3mad3
 hob1nak1
 hob1wsp1
 hob1rad3
 cdr2cdc13
 cdr2cdc2
 shows these two are separated by tab represented as columns
 i have to check the common data between these two coloumn1 an coloumn2
 my code:
 data = []
 data1 = []
 result = []
 fh = open('sheet1','r')
 for line in fh.readlines():
 splitted = line.strip().split('\t')
 data.append(splitted[0])
 data1.append(splitted[1])
 for k in data:
 if k in data1:
 result.append(k)
 print result
 fh.close()

 can you tell me problem with my script and what should is do for this

For a start, you are iterating k in data  *everytime* you iterate a
line in fh  which will give you a speed issue and give you duplicates
in the result.  The following is probably what you intended to do

 for line in fh.readlines():
 do stuff
 for k in data:
 do stuff

.split()  splits by spaces, newlines AND tabs so you just need

 splitted = line.split()

eg

 ln = 'fhl1\tfkh2\r\n'
 ln.split()
['fhl1', 'fkh2']

I think I would have done something like this (not tested)

Input = open('sheet1').read().split()
data = set(Input[::2])
data1 = set (Input[1::2])
result = data.intersection(data1)

or even this (if you don't need data and data1 later in the code)

Input = open('sheet1').read().split()
result = set(Input[::2]).intersection(set (Input[1::2]))


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


Re: Problems with headers in email.message

2007-08-04 Thread Tim Williams
On 04/08/07, Slippy [EMAIL PROTECTED] wrote:
 Hi, python newbie, struggling to learn here. I'm trying to write a
 simple program which posts messages to my google group (via email).
 I'm using smtplib, email and email.message to build and send a
 message, but all the header attributes are appearing in the message
 body, so my messages are arriving with loads of junk in the body, no
 subject line and only the skinniest of headers. I know I'm doing
 something wrong, but I don't know what...

 Here's a simplification of what I'm doingessentially, I've taken
 from what I've learned at
 http://codeidol.com/python/python3/Client-Side-Scripting/pymail-A-Console-Based-Email-Client/

 -Snip Here-Snip Here-Snip
 Here-
 import smtplib, email
 from email.message import Message
 m = Message( )
 m['From'] = 'Slippy [EMAIL PROTECTED]'
 m['To'] = '[EMAIL PROTECTED]'
 m['Subject'] = 'A Test Message'
 m.set_payload('This is a test email. Please ignore')
 s = smtplib.SMTP('smtp.myemail.com')
 failed =
 s.sendmail('[EMAIL PROTECTED]','[EMAIL PROTECTED]',str(m))
 if failed:
  print 'Message sending failed.'
 else:
  print 'Message sent.'
 print 'Bye.'
 -Snip Here-Snip Here-Snip
 Here-

 Now, I get all the right responses, and the message sends ok. I go and
 check my inbox, and the message is there, but the From, To and Subject
 lines I created (as well as a preceding blank line and a From nobody
 line) are in the message body, followed by the body text.

 How do I assign values to the header?

 I'd appreciate any help anyone can give me with this.

Your script (as posted) works fine for me.

  I did need to change one import line to:  from email.Message import
Message (note the capitalization), but that was all - originally it
stopped the script dead, so it wasn't the cause of your problem.


-- 

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


Re: beginner in python

2007-08-02 Thread Tim Williams
On 02/08/07, Beema shafreen [EMAIL PROTECTED] wrote:
 Hi everybody ,
   I am a beginner in python,
  I have to fetch the redundant entries from a file,

 code:

 import re
 L = []
 fh = open('ARCHITECTURE_MAIN.txt',
 'r')
 for line in fh.readlines():
 data =line.strip()
 #   splitted = data.split('#')
 L.append(data)
 fh.close()



 M=L
 for x in L:
 x = x.split('#')
 for y in M:
 y = y.split('#')
 x_data = x[0],x[1],x[2],x[3]
 #print x_data
 y_data = y[0],y[1],y[2],y[3]
 #print y_dat
 if x_data[0] == y_data[0]:
   print x_data


 i get the result as a tupule,
 the text file which has datas separated by hash
 entry#isoform#start#stop#  i have to check upto this

 00250_1#ARCH_104#61#89#Literature#9224948#00250
 00250_1#ARCH_104#97#126#Literature#9224948#00250
 00250_1#ARCH_104#139#186#Literature#9224948#00250
 00251_1#ARCH_463#7#59#SMART##00251
 00251_1#ARCH_463#91#121#SMART##00251
 00251_1#ARCH_463#251#414#SMART##00251
 00251_1#ARCH_463#540#624#SMART##00251
 00252_1#ARCH_474#1#21#Literature#8136357#00252
 00252_1#ARCH_393#481#501#Literature#8136357#00252
 00252_1#ARCH_463#523#553#SMART##00252
 00253_1#ARCH_82#37#362#SMART##00253
 00253_1#ARCH_54#365#522#SMART##00253
 00253_1#ARCH_104#589#617#SMART##00253
 00253_1#ARCH_104#619#647#SMART##00253
 00253_1#ARCH_104#684#712#SMART##00253
 00254_1#ARCH_82#27#352#SMART##00254
 00254_1#ARCH_54#355#510#SMART##00254
 00254_1#ARCH_104#576#604#SMART##00254
 00254_1#ARCH_104#606#634#SMART##00254
 00254_1#ARCH_104#671#699#SMART##00254
 00255_1#ARCH_82#56#425#SMART##00255
 00255_1#ARCH_54#428#582#SMART##00255
 00255_1#ARCH_104#696#724#SMART##00255




 can you suggest me ,what are the improvement i have to make in the above
 code
 regards
 shafreen

Shafreen,   your code snippet (as you posted it) prints any lines that
end with an entry code equal the entry code on the last line of the
file, with these lines split at #.

The whole thing (as you posted it) could probably be written something
like this:

===
# not tested or optimised

fh = open('ARCHITECTURE_MAIN.txt').read().splitlines()
last_code = fh[-1].split('#')[0]
for line in fh:
if out_line.startswith(last_code):
print out_line.split('#')
# it will always print at least the last line of the file
===

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


Re: From D

2007-07-26 Thread Tim Williams
On 26/07/07, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:

 The str.split method has no bearing on this discussion,

 It most certainly does. To make '123 456' into an integer,
 you split it and then join it.
  z = '123 456'
  y = z.split()
  x = ''.join(y)
  w = int(x)
  w
 123456

but it doesn't if you use replace !!   wink

 z = '123 456'
 int( z.replace( ' ' ,'' ) )
 123456


 Propose:
 123 456 789 = 123456789
 123.456 789 = 123.456789

+1 for me too

-- 

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


Re: Gmail Error using smtplib

2007-07-20 Thread Tim Williams
On 20/07/07, DJ Fadereu [EMAIL PROTECTED] wrote:
 Hello, can anyone help me with this? What am I doing wrong here?

 (I've changed private info to /xx)
  I'm getting an authentication error while using a standard script
 Gmail:
 --SCRIPT-
 import smtplib
 from email.MIMEText import MIMEText

 msg = MIMEText('Hello, this is fadereu...')
 From = '[EMAIL PROTECTED]'

 msg['Subject'] = 'Hello'
 msg ['From'] = '[EMAIL PROTECTED]'
 msg['To'] = '[EMAIL PROTECTED]'

 s = smtplib.SMTP('alt1.gmail-smtp-in.l.google.com')
 s.set_debuglevel(1)
 s.login('[EMAIL PROTECTED]','x')
 s.sendmail(msg['From'], msg['To'], msg.as_string())
 s.close()
 ERROR--
 Traceback (most recent call last):
  File C:\Documents and Settings\Acer User\Desktop\Code\S60 scripts
 \Fadereu's Codez\gmail.py, line 13, in module
s.login('[EMAIL PROTECTED]','x'')
  File C:\Python25\lib\smtplib.py, line 554, in login
raise SMTPException(SMTP AUTH extension not supported by
 server.)
 SMTPException: SMTP AUTH extension not supported by server.


The error fairly self-explanitory :)

SMTPException: SMTP AUTH extension not supported by server.

In fact, you are trying to send outbound email through an inbound
email server.  You need to use GMAIL's outbound server.

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


Re: Copy List

2007-07-18 Thread Tim Williams
On 18/07/07, Robert Rawlins - Think Blue
[EMAIL PROTECTED] wrote:


 What's the best way to create a copy of a list? I've seen several method and
 I'm not sure what to use. This will be in a class and one method creates a
 list which I then want to move to the self scope, like so:


listB = listA[:]

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


Re: smtp server simulation using Python

2007-06-18 Thread Tim Williams
On 17/06/07, William Gill [EMAIL PROTECTED] wrote:
 I have a (web) development computer w/o an SMTP server and want to test
 form generated e-mail using a dummy SMTP server that delivers the mail
 message to a file, or better yet, to a text editor instead of actually
 sending it.  Is it possible to extend the DebuggingServer class,and
 override the process_message() method to accomplish this?  If so, any
 suggestions are appreciated.


Search the list archives for SMTPRIG.py  :)

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


Re: avoid script running twice

2007-06-18 Thread Tim Williams
On 18/06/07, Evan Klitzke [EMAIL PROTECTED] wrote:
 On 6/18/07, Robin Becker [EMAIL PROTECTED] wrote:
  I wish to prevent a python script from running twice; it's an hourly job, 
  but
  can take too long.
 
  My simplistic script looks like
 
 
  ...
  def main():
   fn = 'MARKER'
   if os.path.isfile(fn):
   log('%s: hourly job running already' % formatTime())
   else:
   f = open(fn,'w')
   f.write(str(os.getpid()))
   f.close()
   try:
  work()
   finally:
   os.remove(fn)
 
  if __name__=='__main__':
   main()
 
  but it occurs to me that I might be killed with prejudice during the long
  running work(). Is there a smart way to avoid running simultaneously.

 Another method that you can use is to open up a socket on some
 predetermined port (presumably above 1024), and then have your program
 try to connect to that port and talk to the other program to
 determine whether or not to run (or whether to do some of the
 remaining work, etc.).

You don't need to talk to the socket, a second script trying to create
a second socket on the same number will throw an exception and you can
exit the script cleanly without running a second copy.

You can also do this by holding a file open in write mode until the
script has finished.

  try:
   open('lock.txt','w')
   my_script()
 except:
  #print script is already running

If the file is already open the script won't run,   if the script
finshes/crashes or the machine reboots the open file will close.


In both cases if the script finishes normally or crashes, or the
machine is restarted. The lock  (ie socket or open file) is released.

HTH :)






-- 

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


Re: avoid script running twice

2007-06-18 Thread Tim Williams
On 18/06/07, Wildemar Wildenburger [EMAIL PROTECTED] wrote:
 Robin Becker wrote:
  I wish to prevent a python script from running twice; it's an hourly job, 
  but
  can take too long.
 
  [snip]
  but it occurs to me that I might be killed with prejudice during the long
  running work(). Is there a smart way to avoid running simultaneously.
 

 Well I can think of a dumb way: create a temporary file during the
 transaction and have your script check for that before running its main
 body.


 I think thats the most hassle free way of doing it.

If the script finishes abnormally, the file will still exist and
future execution of the script will fail.

:)




-- 

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


Re: avoid script running twice

2007-06-18 Thread Tim Williams
On 18/06/07, Robin Becker [EMAIL PROTECTED] wrote:
 Wildemar Wildenburger wrote:
  Robin Becker wrote:
 .
 
  Well I can think of a dumb way: create a temporary file during the
  transaction and have your script check for that before running its main
  body.
 
 
  I think thats the most hassle free way of doing it.
  /W
 I looked at the temporary files idea, but I'm not certain about the exact
 details. Normally your create a file and then remove it whilst keeping the 
 file
 handle; that allows your program to write to it whilst guaranteeing that it 
 will
 vanish when you die, but this has to be a named file so that the second 
 instance
 can check for it.


Doesn't that prevent it from being already removed? My unix
 experience is long, but fairly primitive.


You don't need to remove the file, write anything to it,  or check for
prior existance :)

If one process has the file open for writing, no other process can
open it for writing at the same time - and this is what you are
checking for!   The exception that the second concurrent open/write
attempt raises tells you that another instance of the script is
running.

This has the added advantage that should something go wrong, such as
an abnormal termination or reboot,  the open/write disappears and the
next time the script runs everything is back to normal.

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


Re: avoid script running twice

2007-06-18 Thread Tim Williams
On 18/06/07, Nick Craig-Wood [EMAIL PROTECTED] wrote:
 Tim Williams [EMAIL PROTECTED] wrote:
   You can also do this by holding a file open in write mode until the
   script has finished.
 
 try:
  open('lock.txt','w')
  my_script()
except:
 #print script is already running

 That only works under windows

   f=open('lock.txt','w')
   g=open('lock.txt','w')
   f.write('hi')
   g.write('ho')
   f.close()
   g.close()
   open('lock.txt').read()
  'ho'
  

 The best cross platform way to create a lock is creating a directory.
 It is atomic on both windows and linux anyway.

  try:
os.mkdir(lock)
  except OSError:
print locked!
  else:
try:
  do_stuff()
finally:
  os.rmdir(lock)

 (untested)


Being a windows-only person, I didn't know that :)

Actually I think I did,  this thread has happened before - a few months ago :)

I would be worried with the directory-exists option for the same
reason I don't use the file-exists method currently.  It is possible
for the directory to exist when the script isn't running, thus
preventing the script from running again until someone notices.

On Windows the open-a-file-for-writing method works well, but as *nix
doesn't work the same way then maybe the socket solution is the best
cross-platform option.  The socket can't exist when the script isn't
running, and if you try and create a duplicate socket you catch the
exception and exit.

IMHO of course.

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


Re: Printing dots in sequence ('...')

2007-05-22 Thread Tim Williams
On 22 May 2007 01:02:31 -0700, beertje [EMAIL PROTECTED] wrote:
 This is a very newbie question for my first post, perhaps
 appropriately.

 I want to print '' gradually, as a progress indicator. I have a
 for-loop that every 10 steps executes:
 print '.',

 This results in something like 'Loading. . . .', whereas I want
 'Loading'

 A pet peeve, I can't for the life of me figure out how to get this
 desired output. How do I print dots - or anything for that matter - in
 sequence without a space being inserted in between?


maybe this:   (on Win32, don't know about *nix)

for x in range(10):
  print '.\b',



-- 

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


Re: Automatic login to website (newbie)

2007-05-15 Thread Tim Williams
On 15 May 2007 06:38:45 -0700, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
 I'm trying to use PAMIE to login to a website:

 import cPAMIE

 # python.org - just a test, works fine
 ie = cPAMIE.PAMIE()
 website = http://www.python.org;
 ie.navigate(website)
 ie.textBoxSet('q', 'pamie')
 ie.buttonClick('submit')

 # expekt.com - this is what I want to do, but it fails
 ie = cPAMIE.PAMIE()
 website = http://www.expekt.com/eng;
 ie.navigate(website)
 ie.textBoxSet('user', 'MyLogin')
 ie.textBoxSet('pass', 'MyPassword')
 ie.buttonClick('actn')

I don't have PAMIE installed nor have I ever used it,   but the login
area is in a frame and I've had problems with other screen scrapers
and frames.

The frame URL is http://www.expekt.com/contenttop.jsp ,  you could try
navigating directly to the frame to see if it helps

website = http://www.expekt.com/contenttop.jsp;
ie.navigate(website)
ie.textBoxSet('user', 'MyLogin')
ie.textBoxSet('pass', 'MyPassword')
ie.buttonClick('actn')

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


Re: find out all threads?

2007-05-12 Thread Tim Williams
On 11/05/07, Sven Rech [EMAIL PROTECTED] wrote:
 Hi,

 I have a written a C program which makes use of python embedding.
 I want to find out all threads that a loaded module has started.
 But I can't find anything about this in the docs. Is it possible?


Without details of your module, its dificult to say,  maybe

t_count = threading.activeCount()
or
t_count = threading.enumerate()

will do what you need
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if a string is empty in python?

2007-05-04 Thread Tim Williams
On 4 May 2007 03:02:37 -0700, Jaswant [EMAIL PROTECTED] wrote:
 This is  a simple way to do it i think


 s=hello

  if(len(s)==0):
  print Empty
  else:
  print s
 
 hello

Not as simple asIf not s: 

and nowhere near as simple asprint s or 'Empty' :) :)


 s = ''
 print s or 'Empty'
Empty
 s = 'hello'
 print s or 'Empty'
hello

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


Re: Can't Get Email Interface Working

2007-04-07 Thread Tim Williams
On 07/04/07, Eric Price [EMAIL PROTECTED] wrote:
 Good grief! And they call a 722-line program simple?! LOL!
 I did what I need to do with a __one_line_shell_script__ LOL!
 Naw, if I have to go through all that, I'll skip on python this time around,
 thank you very much!
 Eric

Yup, its not so simple

Your problem is :

 File /usr/local/lib/python2.4/mimetools.py, line 130, in choose_boundary
   hostid = socket.gethostbyname(socket.gethostname())
socket.gaierror: (8, 'hostname nor servname provided, or not known')

What do you get if you do the following in your interpreter?


 import socket
 socket.gethostname()
'twilliams'
 socket.gethostbyname(socket.gethostname())
'194.129.107.254'

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


Re: way to extract only the message from pop3

2007-04-05 Thread Tim Williams
On 3 Apr 2007 12:36:10 -0700, flit [EMAIL PROTECTED] wrote:
 Hello All,

 Using poplib in python I can extract only the headers using the .top,
 there is a way to extract only the message text without the headers?

for i in range( M.stat()[0] ): # M.stat returns msg-count and mbox size
msg = '\r\n'.join( M.retr(i+1)[1] )#  retrieve the email into string
hdrs,body = msg.split('\r\n\r\n',1)# split it into hdrs  body

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


Re: way to extract only the message from pop3

2007-04-05 Thread Tim Williams
On 05/04/07, Collin Stocks [EMAIL PROTECTED] wrote:

 On 3 Apr 2007 12:36:10 -0700, flit [EMAIL PROTECTED] wrote:
  Hello All,
 
  Using poplib in python I can extract only the headers using the .top,
  there is a way to extract only the message text without the headers?
 

 so get two strings: only headers, and the whole message.
 find the length of the headers, and chop that off the beginning of the whole
 message:

  message=whole_message[len(headers):None]


This way you have to perform 2 downloads,  the headers and the whole
message. Then join them both into strings and subtract one from the
other by slicing or other means.

(other means?   body = whole_message.replace(headers,'' )  or maybe not ! :)  )

The body starts at the first blank line after the Subject: header, in
practice this is the first blank line.   This is a good starting point
for something simple like my earlier suggestion:

   msg = '\r\n'.join( M.retr(i+1)[1] )#  retrieve the email into string
   hdrs,body = msg.split('\r\n\r\n',1)# split it into hdrs  body

If the original poster required the body to be seperated from the
headers (and I received a private reply from the OP to my original
post that suggested it probably was)  then splitting a joined whole
message at the first blank line is sufficient and only requires 1
download without using the email module

If the OP required just the text parts extracted from the message then
it gets a bit trickier, the email module is the way to go but not
quite how a previous poster used it.

Consider an email that routed through my (python) SMTP servers and
filters today,.

Content: ['text/plain', 'text/html', 'message/delivery-status',
'text/plain', 'text/plain', 'text/plain', 'unknown', 'message/rfc822',
'text/plain', 'text/html']

Is text/html  a text part or an html part for this exercise ?  :)

You need to walk the parts and use something like

# part.get_content_maintype() requires a further call
# to get_content_subtype() , so use
# part.get_content_type() instead.

required = ['text/plain', 'text/tab-separated-values']
for part in EMAIL_OBJ.walk():
text_parts = []
if part.get_content_type() in required:
 text_parts.append(part)

print ('\r\n' + '='*76 +'\r\n').join(text_parts)
# print all the text parts seperated by a line of '='
# end

Whether you use the email module or not, you need to join the
retrieved message into a string.  You can use \n   but if you plan to
push the text back out in an email  '\r\n' is required for the SMTP
sending part.  Your client may or may not convert \n to \r\n at
sending time :)

HTH :)

-- 

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


Re: way to extract only the message from pop3

2007-04-05 Thread Tim Williams
On 06/04/07, Tim Williams [EMAIL PROTECTED] wrote:

 Content: ['text/plain', 'text/html', 'message/delivery-status',
 'text/plain', 'text/plain', 'text/plain', 'unknown', 'message/rfc822',
 'text/plain', 'text/html']

I should explain that this was the content in a single email


 # part.get_content_maintype() requires a further call
 # to get_content_subtype() , so use
 # part.get_content_type() instead.

 required = ['text/plain', 'text/tab-separated-values']
 for part in EMAIL_OBJ.walk():
 text_parts = []
 if part.get_content_type() in required:
  text_parts.append(part)

 print ('\r\n' + '='*76 +'\r\n').join(text_parts)
 # print all the text parts seperated by a line of '='
 # end

Content: ['text/plain', 'text/html', 'message/delivery-status',
'text/plain', 'text/plain', 'text/plain', 'unknown', 'message/rfc822',
'text/plain', 'text/html']

Is text/html  a text part or an html part for this exercise ?  :)

You need to walk the parts and use something like

# part.get_content_maintype() requires a further call
# to get_content_subtype() , so use
# part.get_content_type() instead.

required = ['text/plain', 'text/tab-separated-values']
for part in EMAIL_OBJ.walk():
   # text_parts = []   ==  oops, this should be above the for.
   if part.get_content_type() in required:
text_parts.append(part)

   print ('\r\n' + '='*76 +'\r\n').join(text_parts)
   # print all the text parts seperated by a line of '='
# end
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Indentifying the LAST occurrence of an item in a list

2007-04-04 Thread Tim Williams
On 4 Apr 2007 08:58:49 -0700, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
 For any list x, x.index(item) returns the index of the FIRST
 occurrence of the item in x. Is there a simple way to identify the
 LAST occurrence of an item in a list? My solution feels complex -
 reverse the list, look for the first occurence of the item in the
 reversed list, and then subtract its index from the length of the list
 - 1, i.e.

 LastOcc = len(x) - 1 - x[::-1].index(item)

 Is there a simpler solution?

Simpler ?That's subjective. :)

You definitely need to search/iterate a reversed list, or start from
the far end of a non-reversed list.

For fun only.

 t = [0,1,2,3,0]

 def place(t,i):
... for x,y in zip(t,range(len(t)))[::-1]:  
... if x == i:
... return y
... 
 place(t,3)
3
 place(t,0)
4
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is any way to split zip archive to sections?

2007-03-31 Thread Tim Williams
On 30/03/07, Durumdara [EMAIL PROTECTED] wrote:
 Hi!

 I want to create some backup archives with python (I want to write a backup
 application in Python).
 Some package managers (7z, arj, winzip) can create splitted archives (1
 mega, 650, 700 mega, etc).

 Because I want to ftp these results to a ftp server, I want to split large
 volumes to 15 mb sections.

 Can I do it with any python wrapper automatically (like in Cobian), or I
 need to create the large volume, and next split it with another tool?

 Or anybody knows about a command line tool (like 7z, or arj) that can expand
 the splitted archive (and I can add files with them from Python one by one)?


If you are iterating through a list of files to be backed up, and
adding them to a ZIP one-by-one then you could use something like this
 which adds each file until the zip is over 15mb - then it closes the
ZIP and creates the next one.

Not tested or optimised :)
---
import zipfile

archive_num = 1
outfile = zipfile.ZipFile('/zips/archive%s.zip' % archive_num, w)
zsize = 0

for full_name in filelist:
full_name_path = os.path.join(full_name, full_path)

if zsize  15728640 : # 15mb
outfile.close()
archive_num += 1
outfile = zipfile.ZipFile('/zips/archive%s.zip' % archive_num, w)
zsize= 0

outfile.write( full_name_path , full_name_path ,
zipfile.ZIP_DEFLATED) # add the file
zsize += outfile.getinfo(full_name_path).compress_size #  get
compressed size of file

outfile.close()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sending emails to 3 addresses....

2007-03-30 Thread Tim Williams
On 30/03/07, Boudreau, Emile [EMAIL PROTECTED] wrote:

 sendMail('this is the subject line', 'the results: 71 fails, 229 pass, 300
 total.', '[EMAIL PROTECTED], [EMAIL PROTECTED], [EMAIL PROTECTED]')

 def sendMail(subject, body, TO, FROM=[EMAIL PROTECTED]):
 print TO
 HOST = exchange.mycompany.com
 BODY = string.join((
 From: %s % FROM,
 To: %s % TO,
 Subject: %s % subject,
 ,
 body
 ), \r\n)
 server = smtplib.SMTP(HOST)
 server.sendmail(FROM, [TO], BODY)
 server.quit()


Emile,

You are passing the TO addresses as 3 addresses in a single string.
[TO] results in a list containing a single string - not a list
containing 3 individual addresses.

You need to either pass the addresses to the function as a list
containing the 3 addresses as individual strings,  or change

[TO]

to

TO.split(',')


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


Re: Sending emails to 3 addresses....

2007-03-30 Thread Tim Williams
On 30/03/07, Tim Williams [EMAIL PROTECTED] wrote:

Emile,   (slight change to my original reply)

You are passing the TO addresses as 3 addresses in a single string.

[TO] results in a list containing a single string - not a list
containing 3 individual addresses.

You need to either pass the addresses to the function as a list
containing the 3 addresses as individual strings, and remove the
conversion to a list at sending time.  Or change

[TO]

to

TO.split(',')

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


Re: File deletion after 72 hours of creation

2007-03-30 Thread Tim Williams
On 29 Mar 2007 13:40:58 -0700, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
 On Mar 29, 12:02 pm, [EMAIL PROTECTED] wrote:
  Alex I'm looking for a simple method to delete a folder after 72
  Alex Business hours (saturday/sunday doesnt count) since its
  Alex creation. Note that This is on a linux system and I realize that
  Alex it will be the last modified time. These files wont be modified
  Alex since their creation.
 
  Alex Im very confused on how to work with the number easily.
 
  Take a look at the dateutil module.  Its relativedelta object has some
  weekday sense:
 
 http://labix.org/python-dateutil
 
  Skip

 Hey, thanks for the reply, I was able to accomplish it (somewhat ugly)
 using the datetime module alone. Here is my code.
 the part with the timestamp.file is just a plaintext file containg
 #year#month#day that is created when my client side program copies
 files into my directory.

 Just posting the code so if anyone else is wondering how to do this
 ever maybe they can find some of this useful:
 ---

 #!/usr/bin/env python

 import datetime
 import os
 today = datetime.date.today().toordinal()
 serverFiles = os.listdir(/home/webserver/)
 theDirList=[]
 for xfile in serverFiles:
 if os.path.isdir(/home/webserver/ + xfile) == True:
 theDirList.append(xfile)


 for xdir in theDirList:
 foo=open(/home/webserver/+ xdir + /timestamp.file,'r')
 parseMe = foo.readlines()
 fileDate=parseMe[0].split(#)
 fileYear = fileDate[1]
 fileMonth = fileDate[2]
 fileDay = fileDate[3]

 age_of_file = today -
 datetime.date(int(fileYear),int(fileMonth),int(fileDay)).toordinal()
 true_age = age_of_file
 for i in range(age_of_file):
 d=
 datetime.date(int(fileYear),int(fileMonth),int(fileDay)+i)
 ourDay = d.weekday()
 if ourDay == 5 or ourDay == 6:
 true_age = true_age - 1

  print xdir +  :  + str(true_age) +  days old

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


You may have over thought the problem :)

(Not tested)
--
import os, time

pth = '/home/webserver/'
a_day = 60*60*24
today = time.asctime().split()[0]
cutOffDate = time.time() - (3 * a_day)
if today in ['Mon','Tue','Wed']:
cutOffDate = cutOffDate - (2 * a_day)

for f in os.listdir( pth ):
fname = os.path.join(pth ,f)
if os.path.isfile( fname ) and os.path.getmtime(fname)  cutOffDate:
print fname # os.remove(fname)

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


Re: Sorting directory contents

2007-02-20 Thread Tim Williams
On 20/02/07, Wolfgang Draxinger [EMAIL PROTECTED] wrote:
 H folks,

 I got, hmm not really a problem, more a question of elegance:

 In a current project I have to read in some files in a given
 directory in chronological order, so that I can concatenate the
 contents in those files into a new one (it's XML and I have to
 concatenate some subelements, about 4 levels below the root
 element). It all works, but somehow I got the feeling, that my
 solution is not as elegant as it could be:

 src_file_paths = dict()
 for fname in os.listdir(sourcedir):
 fpath = sourcedir+os.sep+fname
 if not match_fname_pattern(fname): continue
 src_file_paths[os.stat(fpath).st_mtime] = fpath
 for ftime in src_file_paths.keys().sort():
 read_and_concatenate(src_file_paths[ftime])

 of course listdir and sorting could be done in a separate
 function, but I wonder if there was a more elegant approach.

 Wolfgang Draxinger
 --
 E-Mail address works, Jabber: [EMAIL PROTECTED], ICQ: 134682867

Are you running on windows?

 i,o,e = os.popen3(dir c:\windows /OD /A-D /B)
 [x.strip() for x in o.readlines() if 'a' in x ]

Gives a list of filenames (but not directories) in date order, that
match a pattern ('a' in x)

 i,o,e = os.popen3(dir c:\windows /O-D /A-D /B)
 [x.strip() for x in o.readlines() if 'a' in x]

Gives the same list but in reverse chronological order.

Something containing an approximation of my poor-writing style below
would do the job.
 i,o,e = os.popen3(dir c:\windows /OD /A-D /B)
 [os.path.join(a_path, x.strip()) for x in o.readlines() if
match_fname_pattern(x)]
['c:/windows/NeroDigital.ini', 'c:/windows/WindowsUpdate.log',
'c:/windows/setupapi.log', 'c:/windows/wiadebug.log',
'c:/windows/wiaservc.log' 

c:\ Dir /? will give you more help

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


Re: While loop with or? Please help!

2007-01-25 Thread Tim Williams
On 25/01/07, Bruno Desthuilliers [EMAIL PROTECTED] wrote:
 Peter Otten a écrit :
  Bruno Desthuilliers wrote:
 
  and simplified again thanks to Python 'in' operator:
  while not usrinp.lower() in yn:
 
  But note that 'in' performs a substring search and therefore yn and 
  would be accepted as valid answers, too.

 Mmm, right. Thanks for the correction.

 =
while not usrinp.lower() in ['y', 'n']:

or better still

while usrinp.lower() not in ['y', 'n']:

:):)


-- 

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


Re: smtplib starttls gmail example - comments?

2007-01-24 Thread Tim Williams
On 24/01/07, py [EMAIL PROTECTED] wrote:
 I would love for anybody to comment on this code with regard to 
 redundancy/efficiency/wordiness or whatever else.
 for instance, do i understand correctly that i cant have a try: else: without 
 an intervening except:?
 -dave

 stdout.write(calling smtp server...)
 try:
 server = SMTP(msgsmtp)
 except:
 stdout.write(FAIL.)   #using .write to avoid the implied \n 
 with print
 else:
 server.set_debuglevel(msgdebug)
 stdout.write(starting tls...)
 server.ehlo(msgfrom)
 try:server.starttls()
 except: stdout.write(FAIL.)
 else:
 server.ehlo(msgfrom)   #neessary duplication (?)
 stdout.write(logging in...)
 try:server.login(msgfrom, msgpass)
 except: stdout.write(FAIL.)
 else:
 stdout.write(sending...)
 try:server.sendmail(msgfrom, msgto, msgtxt + \n.\n)
 except: stdout.write(FAIL.)
 else:
 try:
 server.quit()
 except sslerror:  # a known and largely ignored 
 issue with early EOF in ssl protocol
 stdout.write(success.)
 else:
 stdout.write(success.)
 --


*** Not tested  but should have the same functionality and error
handling as your script ***

this_host = 'myhostname.mydom1.com'
print calling smtp server..., #  the trailing comma removes '\n'
try:
server = smtplib.SMTP(msgsmtp,local_hostname=this_host)
server.set_debuglevel(msgdebug)
print starting tls...,
server.starttls()
server.ehlo(this_host)  # RFC requirement for 2nd EHLO after requesting TLS
print logging in...,
server.login(msgfrom, msgpass)
print sending...,
failed = server.sendmail(msgfrom, msgto, msgtxt + \n.\n)
try:
server.quit()
except: pass
print success.
except:
print FAIL.,

if failed:
print failed:, failed  # some recipients, but not all of them, failed
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: smtplib starttls gmail example - comments?

2007-01-24 Thread Tim Williams
On 24/01/07, BJ Swope [EMAIL PROTECTED] wrote:



 On 1/24/07, Tim Williams [EMAIL PROTECTED] wrote:
 
  On 24/01/07, py [EMAIL PROTECTED] wrote:
   I would love for anybody to comment on this code with regard to
 redundancy/efficiency/wordiness or whatever else.
   for instance, do i understand correctly that i cant have a try: else:
 without an intervening except:?
   -dave
  
   stdout.write(calling smtp server...)
   try:
   server = SMTP(msgsmtp)
   except:
   stdout.write(FAIL.)   #using .write to avoid the implied
 \n with print
   else:
   server.set_debuglevel(msgdebug)
   stdout.write(starting tls...)
   server.ehlo(msgfrom)
   try:server.starttls ()
   except: stdout.write(FAIL.)
   else:
   server.ehlo(msgfrom)   #neessary duplication (?)
   stdout.write(logging in...)
   try:server.login(msgfrom, msgpass)
   except: stdout.write(FAIL.)
   else:
   stdout.write(sending...)
   try:server.sendmail(msgfrom, msgto, msgtxt +
 \n.\n)
   except: stdout.write(FAIL.)
   else:
   try:
   server.quit()
   except sslerror:  # a known and largely
 ignored issue with early EOF in ssl protocol
   stdout.write(success.)
   else:
   stdout.write(success.)
   --
 
 
  *** Not tested  but should have the same functionality and error
  handling as your script ***
 
  this_host = 'myhostname.mydom1.com'
  print calling smtp server..., #  the trailing comma removes '\n'
  try:
  server =
 smtplib.SMTP(msgsmtp,local_hostname=this_host)
  server.set_debuglevel(msgdebug)
  print starting tls...,
  server.starttls()
  server.ehlo(this_host)  # RFC requirement for 2nd EHLO after
 requesting TLS
  print logging in...,
  server.login(msgfrom, msgpass)
  print sending...,
  failed = server.sendmail(msgfrom, msgto, msgtxt + \n.\n)
  try:
  server.quit()
  except: pass
  print success.
  except:
  print FAIL.,
 
  if failed:
  print failed:, failed  # some recipients, but not all of them,
 failed
  --
  http://mail.python.org/mailman/listinfo/python-list


 Both examples have included the cardinal sin in smtp...

  They both send the message text followed by new line dot new line.

  The smtp protocol specifically mentions CRLF dot CRLF.   Please please
 please use \r\n.\r\n in your code...


Ha, yeah I didn't spot that,  but then I wasn't looking for it either :)

However, Its not an issue with smtplib because its takes these kind of
things into account by:

* modifying line endings that aren't RFC compliant to CRLFs.
* normalising/changing a single leading dot (.) to dot dot (..)
* adding the CRLF dot CRLF to the end of the supplied message text.

So adding adding \n.\n to the end of the msg text will just add a
line containing  ..  to the end of the message text, it won't
terminate the DATA part.

that line should therefore be just

failed = server.sendmail(msgfrom, msgto, msgtxt)

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


Re: generate tuples from sequence

2007-01-17 Thread Tim Williams
On 17 Jan 2007 04:50:33 -0800, Will McGugan [EMAIL PROTECTED] wrote:

 Will McGugan wrote:

  Hi,
 
  I'd like a generator that takes a sequence and yields tuples containing
  n items of the sqeuence, but ignoring the 'odd' items. For example

 Forgot to add, for my purposes I will always have a sequence with a
 multiple of n items.

something along the lines of...

 [ (x,x+1,x+2) for x in xrange(0,9,3) ]
[(0, 1, 2), (3, 4, 5), (6, 7, 8)]


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


Re: Python web app. (advice sought)

2007-01-16 Thread Tim Williams
On 16/01/07, Ralf Schönian [EMAIL PROTECTED] wrote:


 I would also like to vote for Karrigell.

 BTW: Does anyone knows how to avoid stopping/starting of the webserver
 after changing external libraries? I have some own modules under
 /opt/local/python/lib and import them by extending the path with
 sys.path.append() After changing any file here, I have to restart
 Karrigell.


Ralf,  you should ask this on the Karrigell list  :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: smtplib question

2007-01-16 Thread Tim Williams
On 16/01/07, gandalf gold [EMAIL PROTECTED] wrote:

 Hi everyone,

 I was trying out smtplib and found out that I can email to anyone in my
 domain but not to an email address not in the domain. From browsing on the
 web, it seems that this has to do with the configuration of the mail server.
 The mail server in my organization is MS exchange. Obviously I can email to
 any email address from my MS outlook.

 What's the easiest way to fix the program?

 Thanks.

 - gan

 import sys, smtplib
 fr = [EMAIL PROTECTED]
 to = [EMAIL PROTECTED] # will not work
 line = testing
 subj = subject line
 msg = From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n%s % (fr, to, subj, line)
 server = smtplib.SMTP(EXCHCLUSTER.ccis.edu)
 server.set_debuglevel(1)
 server.sendmail(fr, [to], msg)
 server.quit()


You will need to get smtplib to authenticate when connecting to the
exchange server.Use the login function and the same username/pw
that you use to login to Outlook.

server.login(user, password)

Note: Your exchange administrator may have disabled this function.  (
Your Outlook will almost certainly be using MAPI not SMTP to talk to
the server).

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


Re: Python web app. (advice sought)

2007-01-15 Thread Tim Williams
On 15 Jan 2007 00:52:33 -0800, Torabisu [EMAIL PROTECTED] wrote:

 Duncan Smith wrote:
  Hello,
   I find myself in the, for me, unusual (and at the moment unique)
  position of having to write a web application.  I have quite a lot of
  existing Python code that will form part of the business logic.  This
  relies on 3rd party libraries (such as numpy) which would make porting
  to e.g. IronPython difficult (I would imagine).  I was thinking LAMP
  (the P standing for Python, of course), particularly as I was originally
  encouraged to go for open source solutions.
 
  The application will provide some basic statistical analyses of data
  contained in database tables and associated plots (R / matplotlib /
  graphviz).  There will also be some heavier duty Monte Carlo simulation
  and graphical modelling / MCMC.  The user(s) will need to be able to set
  model parameters; maybe even tinker with model structure, so it will be
  very interactive (AJAX?).
 
  I've had a look at Django, Turbogears and Plone, and at the moment I am
  torn between Turbogears and Plone.  I get the impression that Turbogears
  will require me to write more non-Python code, but maybe Plone is more
  than I need (steeper learning curve?).  Maybe Turbogears will lead to a
  more loosely coupled app. than Plone?
 
  The disconcerting thing is that others on the project (who won't be
  developing) have started to talk about a LAMP back end with an IIS front
  end, .NET, and the benefits of sharepoint.  The emphasis is supposed to
  be on rapid development, and these technologies are supposed to help.
  But I have no real familiarity with them at all; just Python, C and SQL
  to any realistic level of competence.
 
  Any advice would be greatly appreciated.  I have to do much of the
  statistical work too, so I need to make good choices (and hopefully be
  able to justify them so nobody else on the project makes inappropriate
  choices for me).  e.g. I don't mind learning Javascript if it doesn't
  take too long.  The physical server will initially be a multiprocessor
  machine with several GB of RAM.  But we also have a cluster (I have no
  details, I only started the project a week ago).  So any advice
  regarding parallelisation would also be appreciated (or, in fact, any
  useful advice / pointers at all).  Thanks.
 
  Duncan

 I was in a similar boat a while back, needing to make a decision on
 what to use for our web development.  I had worked with Plone
 previously and found that for our needs it wasn't going to work.  Our
 web development was quite specific and didn't fit ideally into the
 standard content management realm.  I also looked at Django and
 TurboGears, installing and working with each.  I eventually went with
 Django, and I've really enjoyed working with it.  Was a personal choice
 and I'm sure our development would have been as successful if I'd
 chosen TurboGears.

 If you want the strength of persistent layers, MVC, templating etc etc
 but want to stay away from the heavier frameworks, another possibility
 is http://webpy.org/.  Very simple to implement, lightweight yet still
 fairly full of features.


Don't overlook Karrigell either,  with a tiny learning curve its
always worth consideration, especially if you need rapid development
and a web server that will sit on top of your exising .py modules.

www.karrigell.com

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


Re: WMI Python, writing remotely and retrieving env variables values

2007-01-13 Thread Tim Williams
On 13 Jan 2007 02:01:11 -0800, Tim Golden [EMAIL PROTECTED] wrote:
 Thierry Lam wrote:
  I'm using the WMI library for python and I was able to connect to
  another computer on the network with the following line:
 
  c = wmi.WMI(computer=the-network-computer, user=hello,
  password=hello)
 
  Is there a way to write information to a file on that computer?
 
  How do I read environment variables, for example SystemDrive?

 Questions of this sort are really Win32 questions rather than Python
 questions. That's not to say we can't help, but rather that you can
 probably find an answer by going to a search engine of your choice
 and typing in, say, WMI SystemDrive. I did that, and the first hit
 (from Google) was:

 http://msdn2.microsoft.com/en-us/library/aa394239.aspx

 which indicates that it's available from the Win32_OperatingSystem
 WMI class. So, in Python:

 code
 import wmi
 c = wmi.WMI () # optionally on another computer
 for os in c.Win32_OperatingSystem ():
   print os # show the whole thing
   print os.SystemDrive # get only what you want

 /code

 If you were after Environment Variables, then search again,
 this time for WMI Environment Variables. Third hit:

 http://msdn2.microsoft.com/en-us/library/aa394143.aspx

 pointing to the Win32_Environment class. And so on.

 Your first question: can I write into a file? is a little
 more tricky. As far as I know, there's no way even to
 *create* a file with WMI, let alone write information into
 it. It's not really a file-manipulation technology. You can
 get hold of the name of a remote file and other of its
 properties via the CIM_DataFile class, but you'd have
 to translate that into an accessible UNC to be able to
 access it.


One possibility is to create a hidden share on the remote computer and
write to a file on it.

Shares ending in $ are hidden,the unc path would be \\machinename\share$

According to http://msdn2.microsoft.com/en-us/library/aa394594.aspx
you need the WMI Win32_Share class and the Create method.

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


Re: parsing a file name

2007-01-12 Thread Tim Williams
On 12 Jan 2007 09:16:51 -0800, CSUIDL PROGRAMMEr [EMAIL PROTECTED] wrote:
 I have a filename
 cairo-2.3.4.src.rpm
 Is there any way i can only get 2.3.4 from this file name

Is this a one off,  or do you have to process multiple files with similar names?


-- 

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


Re: recursive function

2007-01-08 Thread Tim Williams
On 8 Jan 2007 16:03:53 +0100, Neil Cerutti [EMAIL PROTECTED] wrote:


 len(dict.keys()).


Or

len(dict)

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


Re: Comparing files in a zip to files on drive

2006-12-28 Thread Tim Williams
On 28 Dec 2006 18:35:15 -0800, kj7ny [EMAIL PROTECTED] wrote:
 I am attempting to incrementally back up files using python.  How can I
 compare a file on a hard drive to a file in a python created zip file
 to tell if the file has changed?  Or should I be using some other
 method to determine if a file has changed?

If you are running on an extended FAT or an NTFS filesytem you can
utilise a file's archive attribute. It is set when a file is opened
for modifying.

See the following description of how a backup product describes the process

http://www.grigsoft.com/wndsync/help/sources/abit.htm



-- 

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


Re: win32 service

2006-12-16 Thread Tim Williams
On 16/12/06, g.franzkowiak [EMAIL PROTECTED] wrote:
 Hi everybody,

 have a little problem with a service on Win32.

 I use a TCP server as service, but can't access from an other machine.
 Only local access is possible.

 The service starts like this:

 - myService.py --username user --password password install -

 followed by start

 The user is member in Log on as service, but... only local access :-(

 What is wrong or what can I do ?


Have you checked that your firewall isn't causing the problem,  and
that the servers IP address is accessible from other machines to start
with ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: parsing a dictionary from a string

2006-12-15 Thread Tim Williams
On 15/12/06, Benjamin Georgi [EMAIL PROTECTED] wrote:
 Hello list,

 I could use some help extracting the keys/values of a list of
 dictionaries from a string that is just the str() representation of the
 list (the problem is related to some flat file format I'm using for file
 IO).

 Example:
   s = str(dict_list)
   s
 '[{0: [2], 1: []}, {0: [], 1: [], 2: []}, {0: [1, 2]}]'

 Then, what I want to do is to reconstruct dict_list given s.
 Now, one possible solution would be

   dict_list = eval(s)

 but since the content of s cannot be blindly trusted I`d rather not do
 it that way. Basically my question is whether there is another solution
 which is simpler than using regular expressions.


I'm sure I'll get flamed for the following !!  LOL

You could check that the string contains at least part of what you are
expecting.

if  s[0] == '[' :dict_list = eval(s)

or
if  s[:2] == '[{' :   dict_list = eval(s)
if  s[:2] == '[{' and  s[-2:] == '}]' :  dict_list = eval(s)
etc

I doubt it is perfect,  but it is quick and simple and much safer than
a straight eval()

*or*

try:
dict_list = [eval(x+'}') for x in
s.replace('[{','{').replace('}]','').split('},')]
except:
print file may be compromised
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: 10, 20, 30 to [10, 20, 30]

2006-11-23 Thread Tim Williams
On 23 Nov 2006 03:13:10 -0800, Daniel Austria [EMAIL PROTECTED] wrote:
 Sorry,

 how can i convert a string like 10, 20, 30 to a list [10, 20, 30]

 what i can do is:

 s = 10, 20, 30
 tmp = '[' + s + ']'
 l = eval(tmp)

 but in my opinion this is not a nice solution


Not nice, especially if you can't control what is in s :)

A simple solution if you know s will always contain string
representations of integers is:

 s = 10, 20, 30
 [int(x) for x in s.split(',')]
[10, 20, 30]


Otherwise a good starting point might be:

 for i in s.split(','):

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


Re: 10, 20, 30 to [10, 20, 30]

2006-11-23 Thread Tim Williams
On 23/11/06, Steven D'Aprano [EMAIL PROTECTED] wrote:
 On Thu, 23 Nov 2006 03:13:10 -0800, Daniel Austria wrote:

  Sorry,
 
  how can i convert a string like 10, 20, 30 to a list [10, 20, 30]
 
  what i can do is:
 
  s = 10, 20, 30
  tmp = '[' + s + ']'
  l = eval(tmp)
 
  but in my opinion this is not a nice solution


 It is a dangerous solution if your data is coming from an untrusted source.

  s = 10, 20, 30
  L = [x.strip() for x in s.split(',')]
  L
 ['10', '20', '30']
  L = [int(x) for x in L]
  L
 [10, 20, 30]

 Or, as a one liner:  [int(x.strip()) for x in s.split(',')]

You don't need the strip()

 int('10 ')
10


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


Re: socket.error connection refused

2006-11-23 Thread Tim Williams
On 23 Nov 2006 04:09:18 -0800, Vania [EMAIL PROTECTED] wrote:
 Hi, I'm not sure this is the proper forum but I try nevertheless.
 The problem I'am facing is that the socket library always fail to
 connect to an URL. The net effect is that I can not use setuptools.
 I'm using Python2.4 on a windows XPPRO Sp2 machine.
 The firewall is disabled.
 There is no NLTM proxy.
 I connect to the internet through a NAT server (and it works).
 Other than with easy_install I tried to connect to a number of external
 urls
 (all of them running) and even to localhost,
 directly in script using urllib
 and the error is always the same errno:  10061 connection refused.
 Any ideas?

A socket can't connect to a URL, a URL is an absolute location of an
internet resource, eg hostname + (virtual) location on a server + page
name.  A socket can connect to an IP address or hostname - which is
the first part of the URL after the http://;  (or ftp:// etc)

You need to post a code snippet and the errors you are getting.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: 10, 20, 30 to [10, 20, 30]

2006-11-23 Thread Tim Williams
On 23/11/06, Fredrik Lundh [EMAIL PROTECTED] wrote:
 Tim Williams wrote:
 

 and the use of a list comprehension is pretty silly to, given that you want
 to apply the same *function* to all items, and don't really need to look
 it up for every item:

 map(int,  s.split(','))

Haha, thanks Frederic,  I wondered how long it would take for a reply
from you :)

Silly though ??

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


Re: Getting externally-facing IP address?

2006-11-10 Thread Tim Williams
On 10/11/06, Michael B. Trausch [EMAIL PROTECTED] wrote:



  
  



Every programming example that I have seen thus far shows simple server code and how to bind to a socket--however, every example binds to the localhost address. What I am wondering is this: Is there a clean way to get the networked IP address of the machine the code is running on? For example, my laptop's IP address is 
192.168.0.101, and I want to bind a server to that address. Is there a clean way of doing so that will work, for example, when I move the code to my server (which obviously doesn't have the same IP address)?


Try using 0.0.0.0 as the IP address, or possibly giving IP address at all. HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Getting externally-facing IP address?

2006-11-10 Thread Tim Williams
On 10/11/06, Tim Williams [EMAIL PROTECTED] wrote:


 On 10/11/06, Michael B. Trausch [EMAIL PROTECTED] wrote:
 
 
 
  Every programming example that I have seen thus far shows simple server
 code and how to bind to a socket--however, every example binds to the
 localhost address.  What I am wondering is this:  Is there a clean way to
 get the networked IP address of the machine the code is running on?  For
 example, my laptop's IP address is 192.168.0.101, and I want to bind a
 server to that address.  Is there a clean way of doing so that will work,
 for example, when I move the code to my server (which obviously doesn't have
 the same IP address)?
 
 

 Try  using 0.0.0.0  as the IP address,  or possibly giving IP address at
 all.

 HTH :)



Correction:

- or possibly giving *no* IP address at
-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   3   >