Re: [Tutor] Why does print(a_list.sort()) return None?

2015-03-29 Thread Alan Gauld

On 29/03/15 07:00, Cameron Simpson wrote:


  print(a_list.sort())

is printing the result of "a_list.sort()".

Like most Python functions that operate on something (i.e. .sort, which
sorts the list in place), the .sort method returns None. And that is
printed.


But you can use the sorted() function which returns a
sorted copy of the list. So replace your print statement
with

print(sorted(a_list))

gets you the display you want. But does not sort the original.
So it depends on whether you just want to display it, or
actually want to sort it.
Use either:

a_list.sort()
print(a_list)

OR

print(sorted(a_list))


HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] escape character regex

2015-03-29 Thread Ian D
Ha ha thanks Danny for the hex message!


I am looking to basically match  2 unknown hex values or a byte at the end of 
the 4 byte sequence.

I realise now I am trying to use a numeric \d expression when it needs to be 
matching 2 nibbles or a byte.


Is there a way to match using some sort of wildcard for the last byte as it 
changes?


Thanks 


> Date: Sat, 28 Mar 2015 20:21:09 -0400
> From: da...@davea.name
> To: tutor@python.org
> Subject: Re: [Tutor] escape character regex
>
> On 03/28/2015 03:37 PM, Ian D wrote:
>> Hi
>>
>>
>> I run a regex like this:
>>
>>> pchars = re.compile('\x00\x00\x00') #with or without 'r' for raw
>
> Which one did you actually want? The 3 byte sequence consisting of
> nulls, or the 12 byte one containing zeroes and backslashes? I'm going
> to assume the former, in which case you cannot use 'r' for raw. Unless
> you've got a null key on your keyboard.
>
>>
>> on a string like this:
>>
>>> data = "['broadcast', 'd8on\x00\x00\x00\x11broadcast', 'd11on']"
>>
>>> print "found pchars :",pchars.findall(data)
>>
>> which returns:
>>
>>> found pchars : ['\x00\x00\x00']
>>
>>
>> But if I try to match the extra digits at the end like this:
>>
>>> pchars = re.compile('\x00\x00\x00\x\d+')
>>
>> I get an error:
>>
>>> ValueError: invalid \x escape
>
> The \x escape sequence must be followed by exactly two hex digits, and
> forms a single byte from them. What did you want that byte to be, and
> why didn't you specify it?
>
>>
>> Or if I use another ide than idle it actually flags it as an "illegal 
>> hexadecimal escape sequence"
>>
>
> The question is not what the various IDE's produce, but what the Python
> compiler produces. So once you started getting errors, you really
> should have just run it in the interactive interpreter, without IDE's
> second-guessing you. Anyway, in 2.7.6's interactive interpreter, I get:
>
 a = '\x00\x00\x00\x\d+'
> ValueError: invalid \x escape

>
> So it has nothing to do with re, and is simply the result of trying an
> invalid string literal.
>
> What string were you hoping to get? You mention you wanted to match
> digits at the end (end of what?). Perhaps you wanted a real backslash
> followed by the letter d. In that case, since you cannot use a raw
> string (see my first response paragraph), you need to double the backslash.
>
 a = '\x00\x00\x00\\d+'
 print a
> \d+
>
>
> Your data is funny, too, since it almost looks like it might be a string
> representation of a Python list. But assuming you meant it exactly like
> it is, there is a funny control character following the nulls.
>>
>> How could I match the \x00\x00\x00\x11 portion of the string?
>>
>
> There are no digits in that portion of the string, so I'm not sure why
> you were earlier trying to match digits.
>
> Perhaps you meant you were trying to match the single control character
> x'11'. In that case, you'd want
>
> a = '\x00\x00\x00\x11'
> pchars = re.compile(a)
>
>
> But if you wanted to match an arbitrary character following the nulls,
> you'd want something different.
>
> I think you'd better supply several strings to match against, and show
> which ones you'd expect a match for.
>
> --
> DaveA
> ___
> Tutor maillist - Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>   
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python OO

2015-03-29 Thread Alan Gauld

On 29/03/15 02:16, Juan C. wrote:

Ok, so, let me try to express what I think is 'right' here according to
what you said.

My code structure needs to be something like that:

pycinema
- package: pycinema
- - __init__.py
- - api.py
- - actor.py
- - movie.py
- - serie.py
- __main__.py



What I said only relates to serie.py, You have just introduced
a whole bunch of new concepts that we haven't discussed so far.


1. You said that I need to separate the API methods and calls from my code,
so that if I need to move to another API or something like that, all the
API code and logic will be in one place


That's one way, but what I really said was that you should separate the 
MDB API from the one you expose to your users (eg. your app). One way to 
do that is wrap the MDB API in a class of its own class MDB, say?
You can then subclass or replace the instance of that class if you 
change or use another MDB service later.



2. I've read in many places that a basic structure, package with all the
logic and a single __main__.py on the folder root, is good and simple.


This is good for building a reusable module.
This is different from building an actual app. The app code could
have a different structure, and just import the modules or package
you define above.


Let's start with something more simple, the actor.py module. Currently I
already have this working code (http://pastebin.com/wcCnCwMc) for this
module.

How would be a good way to "convert" this API call to my api.py?

Should I create an API class and have a bunch of methods like "get_actor",
"get_movie", "get_serie", "get_season" and "get_episode"?


Yes, probably. Express the API in generic terms so that if you were 
using another one the same generic requests would apply. Do NOT call the 
API class API! Call it something that reflects what the API represents. 
In this case a Movie DB...



In this code I'm only getting the name of the actor, but I can get other
things from the API like birthday, deathday (when applicable),
place_of_birth, and so on.

Should my api.py get only the RAW data, in this case the JSON, and give it
to my classes (Actor, Movie, Serie) and they (the classes) read this JSON
and do what they need?


No, the data representation is part of the API. Your API class should 
return native Python code that your classes can use. Then if you use a 
new API that returns XML, or CSV, or even uses a local (SQL?) database, 
you can replicate the API methods in a new class but the returned data 
looks the same.



HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] escape character regex

2015-03-29 Thread Ian D
Ok I got it. 

pchars = re.compile(b'\x00\x00\x00[\0-\xff]')

preceeding b and [0-\xff]






> From: dux...@hotmail.com
> To: tutor@python.org
> Date: Sun, 29 Mar 2015 07:55:01 +
> Subject: Re: [Tutor] escape character regex
>
> Ha ha thanks Danny for the hex message!
>
>
> I am looking to basically match 2 unknown hex values or a byte at the end of 
> the 4 byte sequence.
>
> I realise now I am trying to use a numeric \d expression when it needs to be 
> matching 2 nibbles or a byte.
>
>
> Is there a way to match using some sort of wildcard for the last byte as it 
> changes?
>
>
> Thanks
>
> 
>> Date: Sat, 28 Mar 2015 20:21:09 -0400
>> From: da...@davea.name
>> To: tutor@python.org
>> Subject: Re: [Tutor] escape character regex
>>
>> On 03/28/2015 03:37 PM, Ian D wrote:
>>> Hi
>>>
>>>
>>> I run a regex like this:
>>>
 pchars = re.compile('\x00\x00\x00') &with or without 'r' for raw
>>
>> Which one did you actually want? The 3 byte sequence consisting of
>> nulls, or the 12 byte one containing zeroes and backslashes? I'm going
>> to assume the former, in which case you cannot use 'r' for raw. Unless
>> you've got a null key on your keyboard.
>>
>>>
>>> on a string like this:
>>>
 data = "['broadcast', 'd8on\x00\x00\x00\x11broadcast', 'd11on']"
>>>
 print "found pchars :",pchars.findall(data)
>>>
>>> which returns:
>>>
 found pchars : ['\x00\x00\x00']
>>>
>>>
>>> But if I try to match the extra digits at the end like this:
>>>
 pchars = re.compile('\x00\x00\x00\x\d+')
>>>
>>> I get an error:
>>>
 ValueError: invalid \x escape
>>
>> The \x escape sequence must be followed by exactly two hex digits, and
>> forms a single byte from them. What did you want that byte to be, and
>> why didn't you specify it?
>>
>>>
>>> Or if I use another ide than idle it actually flags it as an "illegal 
>>> hexadecimal escape sequence"
>>>
>>
>> The question is not what the various IDE's produce, but what the Python
>> compiler produces. So once you started getting errors, you really
>> should have just run it in the interactive interpreter, without IDE's
>> second-guessing you. Anyway, in 2.7.6's interactive interpreter, I get:
>>
> a = '\x00\x00\x00\x\d+'
>> ValueError: invalid \x escape
>
>>
>> So it has nothing to do with re, and is simply the result of trying an
>> invalid string literal.
>>
>> What string were you hoping to get? You mention you wanted to match
>> digits at the end (end of what?). Perhaps you wanted a real backslash
>> followed by the letter d. In that case, since you cannot use a raw
>> string (see my first response paragraph), you need to double the backslash.
>>
> a = '\x00\x00\x00\\d+'
> print a
>> \d+
>>
>>
>> Your data is funny, too, since it almost looks like it might be a string
>> representation of a Python list. But assuming you meant it exactly like
>> it is, there is a funny control character following the nulls.
>>>
>>> How could I match the \x00\x00\x00\x11 portion of the string?
>>>
>>
>> There are no digits in that portion of the string, so I'm not sure why
>> you were earlier trying to match digits.
>>
>> Perhaps you meant you were trying to match the single control character
>> x'11'. In that case, you'd want
>>
>> a = '\x00\x00\x00\x11'
>> pchars = re.compile(a)
>>
>>
>> But if you wanted to match an arbitrary character following the nulls,
>> you'd want something different.
>>
>> I think you'd better supply several strings to match against, and show
>> which ones you'd expect a match for.
>>
>> --
>> DaveA
>> ___
>> Tutor maillist - Tutor@python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
> ___
> Tutor maillist - Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>   
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] escape character regex

2015-03-29 Thread Peter Otten
Ian D wrote:

> Ok I got it.
> 
> pchars = re.compile(b'\x00\x00\x00[\0-\xff]')
> 
> preceeding b and [0-\xff]

Also possible:

re.compile(b"\0\0\0.", re.DOTALL)

. by default means "any byte except \n", the re.DOTALL flag changes that to 
"any byte".

or

re.compile(b"\0{3}.", re.DOTALL)

{3} means "repeat 3 times".

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python OO

2015-03-29 Thread Dave Angel

On 03/28/2015 09:16 PM, Juan C. wrote:

Ok, so, let me try to express what I think is 'right' here according to
what you said.

My code structure needs to be something like that:

pycinema
- package: pycinema
- - __init__.py
- - api.py
- - actor.py
- - movie.py
- - serie.py
- __main__.py



I'd suggest that you NEVER call a module  __main__.py   The name 
"__main__" is reserved for identifying the script file, and is faked 
during the program initialization.


By using that name for an imported file, you could get some very 
confusing errors later.


--
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python OO

2015-03-29 Thread Mark Lawrence

On 29/03/2015 02:32, Juan C. wrote:

On Sat, Mar 28, 2015 at 10:26 PM Mark Lawrence 
wrote:
If your classes are small put them in one source file, which is clearly
simpler than your proposed structure. Why over-engineer something if
there is no need to?

Well, my classes won't be that small, and separating them by modules would
be easier to maintain, and after all, it's only 4 modules, not 10-15.

My actor related methods should be set apart from my movie, serie related
ones, don't you agree?


No.



Having all the classes (Actor, Movie, Serie) in one module would make it
200, maybe 300 lines long.


We're talking Python, not Java.  Some modules run to 1000s of lines, a 
couple of hundred is just peanuts.


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

Mark Lawrence

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python OO

2015-03-29 Thread Peter Otten
Dave Angel wrote:

> On 03/28/2015 09:16 PM, Juan C. wrote:
>> Ok, so, let me try to express what I think is 'right' here according to
>> what you said.
>>
>> My code structure needs to be something like that:
>>
>> pycinema
>> - package: pycinema
>> - - __init__.py
>> - - api.py
>> - - actor.py
>> - - movie.py
>> - - serie.py
>> - __main__.py
>>
> 
> I'd suggest that you NEVER call a module  __main__.py   The name
> "__main__" is reserved for identifying the script file, and is faked
> during the program initialization.
> 
> By using that name for an imported file, you could get some very
> confusing errors later.

No, the __main__.py module is used to invoke a package:

$ mkdir mypackage
$ echo 'print("Hi")' > mypackage/__main__.py
$ python3 -m mypackage
Hi

In Python 2 you need an __init__.py module to run the above example:

$ python -m mypackage
/usr/bin/python: No module named mypackage
$ touch mypackage/__init__.py
$ python -m mypackage
Hi

See also  (there should be 
better documentation, but I didn't manage to find it...)

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why does print(a_list.sort()) return None?

2015-03-29 Thread boB Stepp
On Sun, Mar 29, 2015 at 2:53 AM, Alan Gauld  wrote:
> On 29/03/15 07:00, Cameron Simpson wrote:
>
>>   print(a_list.sort())
>>
>> is printing the result of "a_list.sort()".
>>
>> Like most Python functions that operate on something (i.e. .sort, which
>> sorts the list in place), the .sort method returns None. And that is
>> printed.
>
>
> But you can use the sorted() function which returns a
> sorted copy of the list. So replace your print statement
> with
>
> print(sorted(a_list))
>
> gets you the display you want. But does not sort the original.
> So it depends on whether you just want to display it, or
> actually want to sort it.
> Use either:
>
> a_list.sort()
> print(a_list)
>
> OR
>
> print(sorted(a_list))

Thanks for chiming in on this, Alan. I had not noticed that sorted()
was an available option. I had focused on available list methods.
While it does not matter if my actual lists do or do not get sorted,
my intent was to just have a sorted view of the list, so your
suggestion works better here and uses one less line of code. Thanks!


-- 
boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why does print(a_list.sort()) return None?

2015-03-29 Thread Mark Lawrence

On 29/03/2015 16:25, boB Stepp wrote:

On Sun, Mar 29, 2015 at 2:53 AM, Alan Gauld  wrote:

On 29/03/15 07:00, Cameron Simpson wrote:


   print(a_list.sort())

is printing the result of "a_list.sort()".

Like most Python functions that operate on something (i.e. .sort, which
sorts the list in place), the .sort method returns None. And that is
printed.



But you can use the sorted() function which returns a
sorted copy of the list. So replace your print statement
with

print(sorted(a_list))

gets you the display you want. But does not sort the original.
So it depends on whether you just want to display it, or
actually want to sort it.
Use either:

a_list.sort()
print(a_list)

OR

print(sorted(a_list))


Thanks for chiming in on this, Alan. I had not noticed that sorted()
was an available option. I had focused on available list methods.
While it does not matter if my actual lists do or do not get sorted,
my intent was to just have a sorted view of the list, so your
suggestion works better here and uses one less line of code. Thanks!




The practice of returning None is standard in Python for anything that 
works in place, so as a parallel see also the difference between 
reverse() and reversed()


As for saving a line of code, I'd much rather write three or four lines 
that I can understand at a glance in six months time [or even tomorrow 
:) ] rather than a potentially obtuse one liner.



Glad you've got this sorted


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

Mark Lawrence

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python OO

2015-03-29 Thread Juan C.
On Sun, Mar 29, 2015 at 3:56 AM Ben Finney 
wrote:
As a side issue: You apparently intend to choose names that are English
language.

If that's true, you should know that “actor”, “movie”, “series” are all
singular.

My bad, it's series indeed.


On Sun, Mar 29, 2015 at 10:33 AM Dave Angel  wrote:
I'd suggest that you NEVER call a module __main__.py The name
"__main__" is reserved for identifying the script file, and is faked
during the program initialization.

By using that name for an imported file, you could get some very
confusing errors later.

As Peter said, I'm using __main__.py so that I can invoke the the app like
'python pycinema' in the console. A bunch of people recommend this approach.


On Sun, Mar 29, 2015 at 11:31 AM Mark Lawrence 
wrote:
No.

We're talking Python, not Java. Some modules run to 1000s of lines, a
couple of hundred is just peanuts.

Ok, so you recommend me to have only 2 modules? One for the MDB API and one
for the app inside the pycinema package and then my __main__.py on the
folder root?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python OO

2015-03-29 Thread Mark Lawrence

On 29/03/2015 18:30, Juan C. wrote:

On Sun, Mar 29, 2015 at 3:56 AM Ben Finney 
wrote:
As a side issue: You apparently intend to choose names that are English
language.

If that's true, you should know that “actor”, “movie”, “series” are all
singular.

My bad, it's series indeed.


On Sun, Mar 29, 2015 at 10:33 AM Dave Angel  wrote:
I'd suggest that you NEVER call a module __main__.py The name
"__main__" is reserved for identifying the script file, and is faked
during the program initialization.

By using that name for an imported file, you could get some very
confusing errors later.

As Peter said, I'm using __main__.py so that I can invoke the the app like
'python pycinema' in the console. A bunch of people recommend this approach.


On Sun, Mar 29, 2015 at 11:31 AM Mark Lawrence 
wrote:
No.

We're talking Python, not Java. Some modules run to 1000s of lines, a
couple of hundred is just peanuts.

Ok, so you recommend me to have only 2 modules? One for the MDB API and one
for the app inside the pycinema package and then my __main__.py on the
folder root?


If that is the best solution for your problem then yes.  I believe it 
was Albert Einstein who said something like "Make it as simple as 
possible, but no simpler".


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

Mark Lawrence

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python OO

2015-03-29 Thread Juan C.
Ok, applying what you guys said I have:

- folder: pycinema
- package: pycinema
- - core.py
- - mdb.py
- __main__.py

Current code on core.py:

# -*- coding: utf-8 -*-

from pycinema.mdb import MovieDB

API = MovieDB('API KEY GOES HERE')

class Actor:
def __init__(self, actor_id):
api = API.get_actor(actor_id)
self.name = api['name']
self.biography = api['biography']
self.actorid = api['id']

class Movie:
def __init__(self):
pass

class Series:
def __init__(self):
pass


Current code on mdb.py:

# -*- coding: utf-8 -*-

import requests

class MovieDB:
def __init__(self, api_key):
self.api_key = api_key

def get_actor(self, actor_id):
response = requests.get('http://api.themoviedb.org/3/person/' +
str(actor_id) + '?api_key=' + str(self.api_key)).json()
return {'name': response['name'], 'id': actor_id, 'biography':
response['biography']}


I think the get_actor return is a bit bad. What would be the best way to
return the data?

Should I use classes inside the MovieDB class (Actor, Movie, Series) and
inside those classes have different methods to get each data, like name,
birthday, bio, etc separately?

Only 1 call to the API gives all this data, example (
http://private-anon-37abaab74-themoviedb.apiary-mock.com/3/person/678). If
I have different methods to get each data separately, I would be calling
this same URL many times, when the best approach would be only 1.

I won't need to get the ID from the API, as the user must provide the ID
when instantiating the Actor.

If you can't read the source code, try here: http://pastebin.com/1DWjxQwH
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python OO

2015-03-29 Thread Alan Gauld

On 29/03/15 20:16, Juan C. wrote:


Current code on core.py:

from pycinema.mdb import MovieDB
API = MovieDB('API KEY GOES HERE')

class Actor:

...


Current code on mdb.py:
import requests

class MovieDB:
...
def get_actor(self, actor_id):
response = requests.get('http://api.themoviedb.org/3/person/'...
return {'name': response['name'], 'id': actor_id, 'biography':
response['biography']}

I think the get_actor return is a bit bad. What would be the best way to
return the data?


As an instance of your actor class would seem reasonable.
If I ask for an actor I expect to get an actor back,
not a dictionary.


Should I use classes inside the MovieDB class (Actor, Movie, Series) and
inside those classes have different methods to get each data, like name,
birthday, bio, etc separately?


No, The API should just be a wrapper around the API you are using. It 
should return the Actor, Movie, etc objects that your application uses.

The whole point of the API is just to allow you to change to (or add)
a different source if needed. (It also shields you from future
changes to the API) It should act as a translator from data source
to application object. Any new source can be wrapped in the same
way so it returns the same core objects.


Only 1 call to the API gives all this data, example (
http://private-anon-37abaab74-themoviedb.apiary-mock.com/3/person/678). If
I have different methods to get each data separately, I would be calling
this same URL many times, when the best approach would be only 1.


So inside your API separate out the single data response into its 
different objects. Call the API in the API init method. Then have 
separate methods to get the actors etc which operate on that

internal stored data. Your objective should be to hide the details
of how the interface works from the application objects.

[ Aside:
If there is a lot of data returned you might even store it temporarily 
in a local data base (SQLite file maybe?) The other methods then 
translate into SQL queries into that database. Refreshing the API then 
involves fetching the latest data and updating the database. But that's 
probably only worthwhile if you are storing many hundreds or thousands 
of records.]



I won't need to get the ID from the API, as the user must provide the ID
when instantiating the Actor.


Can you explain how that works? Does the user create their own
random unique values? Do you use a source of unique keys?
Or could the Actor init() maybe generate an ID for the user?

But without the API knowing the ID, how does the correct data get 
selected? Surely there must be some kind of unique key for that to happen?


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Advice on Strategy for Attacking IO Program

2015-03-29 Thread Saran Ahluwalia
Hello:

Here is what I am trying have my program do:


• Monitor a folder for files that are dropped throughout the day

• When a file is dropped in the folder the program should scan the file

o IF all the contents in the file have the same length

o THEN the file should be moved to a "success" folder and a text file
written indicating the total number of records processed

o IF the file is empty OR the contents are not all of the same length

o THEN the file should be moved to a "failure" folder and a text file
written indicating the cause for failure (for example: Empty file or line
100 was not the same length as the rest).


Below are the functions that I have been experimenting with. I am not sure
how to most efficiently create a functional program from each of these
constituent parts. I could use decorators (sacrificing speed) or simply
pass a function within another function.

[code]
import time
import fnmatch
import os
import shutil


#If you want to write to a file, and if it doesn't exist, do this:

if not os.path.exists(filepath):
f = open(filepath, 'w')

#If you want to read a file, and if it exists, do the following:

try:
f = open(filepath)
except IOError:
print 'I will be moving this to the '


#Changing a directory to "/home/newdir"
os.chdir("/home/newdir")

def move(src, dest):
shutil.move(src, dest)

def fileinfo(file):
filename = os.path.basename(file)
rootdir = os.path.dirname(file)
lastmod = time.ctime(os.path.getmtime(file))
creation = time.ctime(os.path.getctime(file))
filesize = os.path.getsize(file)

print "%s**\t%s\t%s\t%s\t%s" % (rootdir, filename, lastmod, creation,
filesize)

searchdir = r'D:\Your\Directory\Root'
matches = []

def search
for root, dirnames, filenames in os.walk(searchdir):
##  for filename in fnmatch.filter(filenames, '*.c'):
for filename in filenames:
##  matches.append(os.path.join(root, filename))
##print matches
fileinfo(os.path.join(root, filename))


def get_files(src_dir):
# traverse root directory, and list directories as dirs and files as files
for root, dirs, files in os.walk(src_dir):
path = root.split('/')
for file in files:
process(os.path.join(root, file))
os.remove(os.path.join(root, file))

def del_dirs(src_dir):
for dirpath, _, _ in os.walk(src_dir, topdown=False):  # Listing the
files
if dirpath == src_dir:
break
try:
os.rmdir(dirpath)
except OSError as ex:
print(ex)


def main():
get_files(src_dir)
del_dirs(src_dir)


if __name__ == "__main__":
main()


[/code]
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Functions not changing

2015-03-29 Thread Calvin Thai
I'm currently making a pygame using IDLE and i'm having a minor issue with
my functions not changing color. My text is all plain black and its
bothering me. Is there a solution or can i make my program using another
text editor?
(I tried using notepad++ but i don't know how to run the program, it only
lets me open html only?)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] What is wrong with my code?

2015-03-29 Thread Antonia van der Leeuw
Hehey!

I'm learning python on a website called codecademy.com, where I made a
program to decode binary numbers. I guess the site uses a different
compiler, because on the site my code worked fine, but when I copied and
pasted it into the Python IDLE (3.4.2) it didn't work! I'm really don't know
what is wrong with my code, can anyone of you fine sirs help me? 

 

Meh code:

 

number_input = input("What binary number do you want me to decode? ")

 

def decoder(number):

number_backwards = str(number)[::-1] # Because binary numbers go from
right to left.

result = 0

value = 1

br = False

for n in number_backwards:

if n != "1" and n != "0":

print number, "is not a binary number"

br = True

break

elif n == "1":

result += value

value += value

if br == False:

print "The binary number decoded is", result

 

decoder(number_input)

 

Thank you! ^^

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Functions not changing

2015-03-29 Thread Calvin Thai
Found the solution

On Sun, Mar 29, 2015 at 2:56 PM, Calvin Thai  wrote:

> I'm currently making a pygame using IDLE and i'm having a minor issue with
> my functions not changing color. My text is all plain black and its
> bothering me. Is there a solution or can i make my program using another
> text editor?
> (I tried using notepad++ but i don't know how to run the program, it only
> lets me open html only?)
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] lamda in list comp

2015-03-29 Thread Jim Mooney
Shouldn't this give me a list of squares?
[lambda x: x**2 for x in range(10)]

Instead I'm getting ten of these (with different addresses)
. at 0x01262A50>]

-- 
Jim

I can't think of a clever tagline today, so just imagine I said something
clever.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Feedback on Script for Pandas DataFrame Written into XML

2015-03-29 Thread Saran Ahluwalia
Hello:

I would appreciate your feedback on whether I correctly wrote my XML. I am
exporting a DataFrame and writing into a XML file. I used the ElementTree
library. The DataFrame has 11 rows and 8 columns (excluding the index
column).

#My schema assumption:
#
#[
#Some number row
#Sample text 
#]
#
CODE: SELECT ALL 

document = ET.Element("list")

def make_message(document, row):
msg = ET.SubElement(document, "message")
for field in row.index:
field_element = ET.SubElement(msg, field)
field_element.text = row[field]
return msg

def add_to_document(row):
return make_message(document, row)

#df.apply(add_to_document, axis=0) ---> if I were to import a DataFrame
stored in the variable
#"df", I would simply APPLY the add_to_document function and COMBINE this
into a document

ET.dump(document)

Thank you, in advance for your help.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Starting the IDLE

2015-03-29 Thread Troll Worfolk
 I downloaded Python 3.4.3 today (twice) and I can't get the IDLE to start.  
The Python in the command window works great but I can't figure how to start 
the IDLE.  When I installed python there are two icons,  'PYTHON'  and 
"PYTHONW" . The first will opens Python in a command window and all is working. 
 The second does nothing.  I've downloaded python twice today and still no 
IDLE.  What am I doing wrong??  I'm working on Windows 7 pro (32 bit). Also 
this is true on a second Windows 7 pro (64bit)  computer I have.
 Troll Worfolk
ph: 928.634.8016
e-mail: t.r...@sbcglobal.net
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] if statement help

2015-03-29 Thread Priya Persaud
Hello,

I recently learned about if statements and math functions. For my class we
are unit testing absolute values and have to make if statements so the
tests pass, but we cannot use built in functions like (abs). How do I start
it, I am so lost.

Thank you
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] script

2015-03-29 Thread George Bollen
hi, i am new to python and i have not done programming before but i am
learning along the way,

just want you to help me on how i can write a script that accepts an input
a measurement of a time interval in
seconds and prints a sentence giving the equivalent of the given number of
seconds
in hours, minutes, and seconds.

thanks.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] lamda in list comp

2015-03-29 Thread Mark Lawrence

On 29/03/2015 02:25, Jim Mooney wrote:

Shouldn't this give me a list of squares?
[lambda x: x**2 for x in range(10)]

Instead I'm getting ten of these (with different addresses)
. at 0x01262A50>]



That's exactly what you've asked the code to do.  What makes you think 
you need a lambda function?


[x**2 for x in range(10)]

should do the trick.

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

Mark Lawrence

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python OO

2015-03-29 Thread Juan C.
On Sun, Mar 29, 2015 at 8:55 PM Alan Gauld 
wrote:
Can you explain how that works? Does the user create their own
random unique values? Do you use a source of unique keys?
Or could the Actor init() maybe generate an ID for the user?

But without the API knowing the ID, how does the correct data get
selected? Surely there must be some kind of unique key for that to happen?


The ID is set by the API, I don't have control over it, I can't modify it,
and I don't really need to modify it. There is an option to search the API
using the name, like so (
http://private-anon-37abaab74-themoviedb.apiary-mock.com/3/search/person?query=Will%20Smith
).

Of course in the "user side" of the things I will ask for names, let's say.
The person wants to know all the movies that Will Smith played in. Of
course I won't ask the user for his ID (that is 287) because they wouldn't
know, the user will simply type his name.

And my code will call this part of the API to get the ID of the person, and
then create a new Actor using 'actor = Actor(287)'.

The same goes for Movie and Series related stuff, I just modify the
"/person?" to "/movie?" or "/tv?".
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] lamda in list comp

2015-03-29 Thread Danny Yoo
On Sat, Mar 28, 2015 at 6:25 PM, Jim Mooney  wrote:
> Shouldn't this give me a list of squares?
> [lambda x: x**2 for x in range(10)]
>
> Instead I'm getting ten of these (with different addresses)
> . at 0x01262A50>]


Mark Lawrence's answer shows how to correct this.


Let's look at the problem a little more closely.  To do so, one thing
we can do is rewrite to try removing the use of lambdas.  Here's a
fairly similar situation to what you had:

##
def square(x):
return x * x

[square for x in range(10)]
##

If you saw that last expression, you might notice a mistake: we
haven't called the function on an argument!

We wanted to say:

[square(x) for x in range(10)]

Once we understand this, let's go back to your original expression:

[lambda x: x**2 for x in range(10)]

If we wanted to revise this so that it applied the function, we can do that:

[(lambda x: x**2)(x) for x in range(10)]


If you're a functional programmer, Python's list comprehension syntax
will take a slight re-adjustment.  List comprehensions do sort-of the
same sort of thing as a functional map, but since it's built-in
syntax, it does a bit more, in that the "mapper" is an expression that
directly involves the mapped values.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is wrong with my code?

2015-03-29 Thread Danny Yoo
On Fri, Jan 23, 2015 at 1:40 PM, Antonia van der Leeuw
 wrote:
> Hehey!
>
> I'm learning python on a website called codecademy.com, where I made a
> program to decode binary numbers. I guess the site uses a different
> compiler, because on the site my code worked fine, but when I copied and
> pasted it into the Python IDLE (3.4.2) it didn't work! I'm really don't know
> what is wrong with my code, can anyone of you fine sirs help me?


Hi Antonia,

Python 3 is a different language than Python 2.  It looks like the
codeacademy materials use Python 2, so you should probably do the same
on your local system.

By the way, next time you ask a question where something goes wrong,
also show us what went wrong.  Be descriptive!  When you learn to
program, you'll find that programs break for all sorts of crazy
pedantic reasons.  Normally, a Python program will explain *why* it
broke.  In your specific situation, it should have said something that
points to one of the lines of your program, with a particular error
message.  When you see this and ask for help, copy and paste the error
message.  It will help.


Good luck to you!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] script

2015-03-29 Thread Danny Yoo
On Sat, Mar 28, 2015 at 4:04 PM, George Bollen  wrote:
> hi, i am new to python and i have not done programming before but i am
> learning along the way,
>
> just want you to help me on how i can write a script that accepts an input
> a measurement of a time interval in
> seconds and prints a sentence giving the equivalent of the given number of
> seconds
> in hours, minutes, and seconds.


Given this is homework, we can't answer your question by just spitting
out a program.  Rather, we'll try to point you toward resources and
give suggestions.


Can you show us what you've tried?  Can you point at any programs
you've seen so far?  Maybe some program that you're seen might be
similar in concept to the problem you're trying to solve.


Also, at a high level of problem solving, it might be useful to look
at George Polya's "How to Solve It" in your local library.  There are
general techniques that people use to solve problems, and they need to
be better known.  Not sure if you've encountered material that shows
these strategies, but you might want to look at them at some point.


Please make sure to keep replies directed at the mailing list.  Good
luck to you.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if statement help

2015-03-29 Thread Danny Yoo
On Thu, Mar 26, 2015 at 5:42 PM, Priya Persaud  wrote:
> Hello,
>
> I recently learned about if statements and math functions. For my class we
> are unit testing absolute values and have to make if statements so the
> tests pass, but we cannot use built in functions like (abs). How do I start
> it, I am so lost.
>
> Thank you


There are multiple situations where we need to teach the computer to
compute a function, where it doesn't know about the function already.
Your teacher appears to be trying to teach you how to build basic
functions, which is admirable.


Pretend that you're talking to someone who doesn't know what the
absolute value function does.

  * Can you say what the absolute value function does?  What's its purpose?

  * Can you give a few examples of inputs and expected outputs of the function?


By the way, if you have some free time, you might find the following
book helpful: http://www.ccs.neu.edu/home/matthias/HtDP2e/  It's not
Python, but it tries to teach how to tackle the kind of programming
problems you'll see in a beginner's class.  What's good about it is
that it concentrates on methodology and problem-solving strategies,
all to keep yourself from getting lost.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is wrong with my code?

2015-03-29 Thread Ben Finney
Danny Yoo  writes:

> Python 3 is a different language than Python 2. It looks like the
> codeacademy materials use Python 2, so you should probably do the same
> on your local system.

Alternatively, learn Python 3 (which at this time means learning
somewhere other than Codecademy).

https://wiki.python.org/moin/BeginnersGuide/NonProgrammers>

Python 2 is only ever going to be the past, don't learn it until you
need to. Learning Python 3 is essential to keep your knowledge relevant
today, and I strongly recommending learning Python 3 *first*.

-- 
 \   “The Vatican is not a state.… a state must have people. There |
  `\are no Vaticanians.… No-one gets born in the Vatican except by |
_o__)an unfortunate accident.” —Geoffrey Robertson, 2010-09-18 |
Ben Finney

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Starting the IDLE

2015-03-29 Thread Danny Yoo
On Thu, Mar 26, 2015 at 12:55 PM, Troll Worfolk  wrote:
>  I downloaded Python 3.4.3 today (twice) and I can't get the IDLE to start.

Did you run the installer afterwards?

What menu items do you see in your Windows "Start Menu" that are
associated to "Python 3.4"?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] trying to convert pycurl/html to ascii

2015-03-29 Thread bruce
Hi.

Doing a quick/basic pycurl test on a site and trying to convert the
returned page to pure ascii.

The page has the encoding line



The test uses pycurl, and the StringIO to fetch the page into a str.

pycurl stuff
.
.
.
foo=gg.getBuffer()

-at this point, foo has the page in a str buffer.


What's happening, is that the test is getting the following kind of error/

UnicodeDecodeError: 'utf8' codec can't decode byte 0xa0 in position 20:
invalid start byte

The test is using python 2.6 on redhat.

I've tried different decode functions based on different
sites/articles/stackoverflow but can't quite seem to resolve the issue.

Any thoughts/pointers would be useful!

Thanks
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python OO

2015-03-29 Thread Juan C.
On Sun, Mar 29, 2015 at 9:39 PM Juan C.  wrote:
The ID is set by the API, I don't have control over it, I can't modify it,
and I don't really need to modify it. There is an option to search the API
using the name, like so (
http://private-anon-37abaab74-themoviedb.apiary-mock.com/3/search/person?query=Will%20Smith
).

Of course in the "user side" of the things I will ask for names, let's say.
The person wants to know all the movies that Will Smith played in. Of
course I won't ask the user for his ID (that is 287) because they wouldn't
know, the user will simply type his name.

And my code will call this part of the API to get the ID of the person, and
then create a new Actor using 'actor = Actor(287)'.

The same goes for Movie and Series related stuff, I just modify the
"/person?" to "/movie?" or "/tv?".


Oh, and yes, my bad, indeed, the user will provide the name only, my script
will get the ID.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is wrong with my code?

2015-03-29 Thread Dave Angel

On 01/23/2015 04:40 PM, Antonia van der Leeuw wrote:

Hehey!

I'm learning python on a website called codecademy.com, where I made a
program to decode binary numbers. I guess the site uses a different
compiler, because on the site my code worked fine, but when I copied and
pasted it into the Python IDLE (3.4.2) it didn't work!


When asking a question here, it's really more useful to say in what way 
it didn't work.  Like if you crashed with an exception, show the stack 
trace including the error.


Still, it's a pretty safe guess that you got an exception on the print 
statement(s), which is a function in Python 3.x.


 I'm really don't know

what is wrong with my code, can anyone of you fine sirs help me?



Meh code:



number_input = input("What binary number do you want me to decode? ")



def decoder(number):

 number_backwards = str(number)[::-1] # Because binary numbers go from
right to left.

 result = 0

 value = 1

 br = False

 for n in number_backwards:

 if n != "1" and n != "0":

 print number, "is not a binary number"



 print(number, "is not a binary number")


 br = True

 break

 elif n == "1":

 result += value

 value += value

 if br == False:

 print "The binary number decoded is", result



  print("The binary number decoded is", result)



decoder(number_input)

--

DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] trying to convert pycurl/html to ascii

2015-03-29 Thread Dave Angel

On 03/29/2015 09:49 PM, bruce wrote:

Hi.

Doing a quick/basic pycurl test on a site and trying to convert the
returned page to pure ascii.


You cannot convert it to pure ASCII.  You could replace all the invalid 
characters with some special one, like question marks.  But I doubt if 
that's what you really want.




The page has the encoding line




That would mean you should use 8859 in your decode.



The test uses pycurl, and the StringIO to fetch the page into a str.

pycurl stuff
.
.
.
foo=gg.getBuffer()

-at this point, foo has the page in a str buffer.


What's happening, is that the test is getting the following kind of error/

UnicodeDecodeError: 'utf8' codec can't decode byte 0xa0 in position 20:
invalid start byte


That's not the whole error.  You need to show the whole stack trace, not 
just a single line.  It would also be really useful if you showed the 
lines between the  foo= line and the one that gets the error.





The test is using python 2.6 on redhat.


Very good to tell us that.  It makes a huge difference.


I've tried different decode functions based on different
sites/articles/stackoverflow but can't quite seem to resolve the issue.



Pick one, show us the code, and show us the full error traceback, and 
somebody can help.  As it stands all I can tell us is a decode takes a 
byte string and an encoding name, and produces a unicode object.  And 
it's not going to give you a utf-8 error if you're trying to decode 8859.


--
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] trying to convert pycurl/html to ascii

2015-03-29 Thread Cameron Simpson

On 29Mar2015 21:49, bruce  wrote:

Doing a quick/basic pycurl test on a site and trying to convert the
returned page to pure ascii.


And if the page cannot be representing in ASCII?


The page has the encoding line

The test uses pycurl, and the StringIO to fetch the page into a str.


Which StringIO? StringIO.StringIO or io.StringIO? In Python 2 the format is 
effectively bytes (python 2 str) and the latter is unicode (as it is in python 
3).



pycurl stuff
foo=gg.getBuffer()
-at this point, foo has the page in a str buffer.
What's happening, is that the test is getting the following kind of error/
UnicodeDecodeError: 'utf8' codec can't decode byte 0xa0 in position 20:
invalid start byte


Please show us more of the code, preferrably a complete example as small as 
possible to reproduce the exception. We have no idea what "gg" is or how it was 
obtained.



The test is using python 2.6 on redhat.
I've tried different decode functions based on different
sites/articles/stackoverflow but can't quite seem to resolve the issue.


Flailing about on stackoverflow sounds a bit random.

Have you consulted the PycURL documentation, especially this page:

 http://pycurl.sourceforge.net/doc/unicode.html

which looks like it ought to discuss your problem.

Cheers,
Cameron Simpson 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Advice on Strategy for Attacking IO Program

2015-03-29 Thread Martin A. Brown


Greetings,


Here is what I am trying have my program do:

• Monitor a folder for files that are dropped throughout the day

• When a file is dropped in the folder the program should scan the file

o IF all the contents in the file have the same length

o THEN the file should be moved to a "success" folder and a text file
written indicating the total number of records processed

o IF the file is empty OR the contents are not all of the same length

o THEN the file should be moved to a "failure" folder and a text file
written indicating the cause for failure (for example: Empty file or line
100 was not the same length as the rest).


This is a very good description of the intent of the programmer. 
Thanks for including this.


Below are the functions that I have been experimenting with. I am 
not sure how to most efficiently create a functional program


Do you mean 'functional programming' [0] style as in Scheme, Haskell 
and/or ML?  Or do you simply mean, 'functioning program'?


from each of these constituent parts. I could use decorators 
(sacrificing speed) or simply pass a function within another 
function.


Given what you describe, I think your concern about sacrificing 
speed is worth forgetting (for now).  Unless we are talking 
thousands of files per minute, I think you can sleep comfortably 
that decorators (which are, operationally, nothing more than another 
function call) are not going to slow down your program terribly.


If you are (ever) worried about speed, then profile and understand 
where to place your effort.  (I belong to the camp that searches for 
correctness first and then performance later.)


Are you aware that you can define functions yourself?  And, that you 
could have your function call one of the below functions?  I will 
demonstrate by changing your 'fileinfo' function slightly.


I snipped the rest of your code.


def fileinfo(file):
   filename = os.path.basename(file)
   rootdir = os.path.dirname(file)
   lastmod = time.ctime(os.path.getmtime(file))
   creation = time.ctime(os.path.getctime(file))
   filesize = os.path.getsize(file)

   print "%s**\t%s\t%s\t%s\t%s" % (rootdir, filename, lastmod, creation,
filesize)


Do you realize you can actually return the values to the caller 
rather than print them?  This is an excelent way to pass information 
around a program.


Without data to examine here, I can only guess based on your 
language that fixed records are in the input.  If so, here's a 
slight revision to your program, which takes the function fileinfo 
as a starting point and demonstrates calling a function from within 
a function.  I tested this little sample on a small set of files 
created with MD5 checksums.  I wrote the Python in such a way as it 
would work with Python 2.x or 3.x (note the __future__ at the top).


There are so many wonderful ways of failure, so you will probably 
spend a bit more time trying to determine which failure(s) you want 
to report to your user, and how.


The only other comments I would make are about safe-file handling.

  #1:  After a user has created a file that has failed (in
   processing),can the user create a file with the same name?
   If so, then you will probably want to look at some sort
   of file-naming strategy to avoid overwriting evidence of
   earlier failures.

File naming is a tricky thing.  See the tempfile module [1] and the 
Maildir naming scheme to see two different types of solutions to the 
problem of choosing a unique filename.


Good luck with your foray,

-Martin

Code snippet:

#! /usr/bin/python

from __future__ import print_function

import os
import time

RECORD_LENGTH = 32

def process_data(f, filesize):
f.seek(0, os.SEEK_SET)
counter = 0

# -- are the records text?  I assume yes, though your data may differ.
#if your definition of a record includes the newline, then
#you will want to use len(record) ...
#
for record in f:
print("record: %s" % ( record.strip()), file=sys.stderr)
if RECORD_LENGTH == len(record.strip()):  # -- len(record)
counter += 1
else:
return False, counter
return True, counter


def validate_and_process_data(f, filesize):
f.seek(0, os.SEEK_END)
lastbyte = f.tell()
if lastbyte != filesize:
return False, 0
else:
return process_data(f, filesize)

def validate_files(files):
for file in files:
filename, rootdir, lastmod, creation, filesize = fileinfo(file)
f = open(filename)
result, record_count = validate_and_process_data(f, filesize)
if result:
# -- Success!  Write text file with count of records
#recognized in record_count
print('success with %s on %d records' % (filename, record_count,), 
file=sys.stderr)
pass
else:
# -- Failure; need additional logic here to determine
#the cause for failure.
print('failure w

Re: [Tutor] Feedback on Script for Pandas DataFrame Written into XML

2015-03-29 Thread Martin A. Brown


Good evening again,

I'm replying to your second post, because I replied to the first. 
This may be a more specific request than is typically handled on 
Python tutor.  This involves specific knowledge of the 
xml.etree.ElementTree and pandas.DataFrame objects.


I would appreciate your feedback on whether I correctly wrote my 
XML. I am exporting a DataFrame and writing into a XML file. I 
used the ElementTree library. The DataFrame has 11 rows and 8 
columns (excluding the index column).


Side note:  Hard to know or give any advice without considerably 
more detail on the data involved.  But



#My schema assumption:
#
#[
#Some number row
#Sample text 
#]
#


That shows 6 (XML) elements.  This is neither 8 nor 11.


CODE: SELECT ALL 

document = ET.Element("list")

def make_message(document, row):
   msg = ET.SubElement(document, "message")
   for field in row.index:
   field_element = ET.SubElement(msg, field)
   field_element.text = row[field]
   return msg

def add_to_document(row):
   return make_message(document, row)

#df.apply(add_to_document, axis=0) ---> if I were to import a DataFrame
stored in the variable
#"df", I would simply APPLY the add_to_document function and COMBINE this
into a document

ET.dump(document)

Thank you, in advance for your help.


This is a more general inquiry and is probably better suited for the 
lxml (ElementTree) mailing list ...


  https://mailman-mail5.webfaction.com/listinfo/lxml

... or maybe the Pandas mailing list:

  https://groups.google.com/forum/#!forum/pydata

Best of luck,

-Martin

--
Martin A. Brown
http://linux-ip.net/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor