[Tutor] Object creation query

2019-08-09 Thread mhysnm1964
  All,

 

I think I am asking for the impossible here. But I will ask anyway.

 

I am using flask_sqlalchemy to build the tables and perform the queries,
updates and insertions. I have multiple tables with the same structure with
different names. A table called accounts which stores the name of the tables
with the same structures. This is the important bits to know about. 

 

I have a page called transactions. When I call this page, I can append
different names to the end. For example:

 

Transactions/account1

Transactions/account2

Transactions/account3

.

 

In the view for transactions I am doing the following (code extract)

 

@app.route('/transactions/')

def transactions(account):

if accounts != "Transactions":

Accounts.query.filter_by(account_name =account).first_or_404()

tables = Accounts.query.all()

if account == 'Account1':

records = Account1

elif account == 'Account2':

records = Account2

records = records.query.order_by(records.date.desc)

 

as I am saving each model object into the same variable depending on the
full URL name. I am wondering if there is a better way in doing this rather
than using a list of if tests? 

 

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


[Tutor] flask_sqlalchemy query in relation to SQL relationships.

2019-08-07 Thread mhysnm1964
All,

 

Python 3.6 under windows 10 - I am using flask_sqlalchemy   and finding it
quite good with taking a lot of the heavy lifting of writing SQL statements.
I have a question in relation to SQL relationships and how would this be
done using SQL or flask_sqlalchemy or sqlalchemy.

 

I have three tables which are related to each other.

 

Table 1 has many records related to table 2.

Table 2 has many relationships to table 3.

 

Below is the model used by flask_sqlalchemy to make things clearer:

 

 

class Categories(db.Model):

# one to many relationships to SubCategories 

id = db.Column(db.Integer, primary_key=True)

category  = db.Column(db.String(64), index=True, unique=True)

subcategories  = db.relationship('SubCategories', backref='categories',
lazy='dynamic')



def __repr__(self):

return ''.format(self.category)

 

class SubCategories(db.Model):

# Many to one relationship to Categories.

# One to many relationship with Transactions.

id = db.Column(db.Integer, primary_key=True)

subcategory  = db.Column(db.String(80), index=True, unique=True)

category_id = db.Column(db.Integer, db.ForeignKey('categories.id'))

transactions   = db.relationship('Transactions',
backref='sub_categories', lazy='dynamic')

 

class Transactions (db.Model):

# Many to one relationship to Sub_categories 

# Many to one relationships with Accounts. 

id  = db.Column(db.Integer, primary_key=True)

transactiondate = db.Column(db.Date, index=True, nullable=False)

description  = db.Column(db.String(80), nullable=False)

amount = db.Column(db.Float, nullable=False) 

subcategory_id = db.Column(db.Integer,
db.ForeignKey('sub_categories.id'))

account_no  = db.Column(db.Integer,
db.ForeignKey('accounts.account_number'))

 

Thus, when I have a query from the view:

 

records = Transactions.query.order_by(Transactions.subcategory_id,
Transactions.transactiondate.desc())

 

page = request.args.get('page', 1, type=int)

records  = records.paginate(page, app.config['POSTS_PER_PAGE'], False)

next_url = url_for('index', page=records.next_num) if records.has_next
else None

prev_url = url_for('index', page=records.prev_num) if records.has_prev
else None

return render_template('index.html', title='Budget Program Main Page',
records = records.items, tables = tables, account  = 'all Transactions',
prev_url = prev_url, next_url = next_url, form = form, sort_form =
sort_form)

 

Template HTML code which displays the category and sub_category text values
based upon the above query. 

{% for row in records %}



{{row.accounts.account_name}}

{{row.account_no}}

{{row.transactiondate}}

{{row.description}}

{{row.amount}}

{{row.sub_categories.categories.category}}

{{row.sub_categories.subcategory }}



{% endfor %}

 

What I cannot do, is use sub_categories.categories.category or
sub_categories.subcategory in the query statements to sort the transaction
table by category or sub_category . For example the following does not work

 

sort_by = Transactions.sub_categories.categories.category

records = Transactions.query.order_by(sort_by.desc())

 

sqlalchemy  complains that it cannot find the object .categories. Thus I do
not know how to sort on the category table which is a child of
sub_categories which is a child of transactions. How would this be done in
SQL?

 

I hope this is the right place for this question.

 

Sean 

 

 

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


[Tutor] Flask and flask_wtf issue -- I hpe someone can help.

2019-08-05 Thread mhysnm1964
All,

 

Thanks to the starting point from Allan's chapter on web ffframeworks. I
have now build quite a nice little app showing a bunch of records from a
sqlite database in tables. The issue I am having is with flask_wtf and
forms. I have a Selector HTML which I want to grab the selected item from
the list of options. I cannot get the data from the selector.

If there is five items and I select the 2nd option. I want the value related
to option 2 to be retrieved and updating a database. Below is the
information I hope will make things clear.

 

 

HTML template:

 

{% extends "base.html" %}

 

{% block content %}

New  Sub Category



{{ form.hidden_tag() }}

{{ form.categoriesfield.label }}

{{ form.categoriesfield(rows=10) }}



{{ form.subcategoryfield.label }}

{{ form.subcategoryfield(size=64) }}

{% for error in form.subcategoryfield.errors %}

[{{ error }}]

{% endfor %}



{{ form.submit() }}



List of sub Categories



{% if prev_url %}

Newer Records

{% endif %}

{% if next_url %}

Next Records

{% endif %}



 Records   



ID

Sub Category Name

Category Name



{% for row in records %}



{{row.id }}

{{row.subcategory }}

{{row.categories.category  }}



{% endfor %}



{% if prev_url %}

Newer Records

{% endif %}

{% if next_url %}

Next Records

{% endif %}



{% endblock %}

 

Now for the form.py class related to this form:

 

 

from flask_wtf import FlaskForm

from wtforms import StringField,  BooleanField, SubmitField, TextAreaField,
TextField, IntegerField, RadioField, SelectField, SelectMultipleField

from wtforms.validators import ValidationError, DataRequired, EqualTo,
Length

from app.models import  * # all table model classes 

 

class SubCategoryForm (FlaskForm):

categoriesfield = SelectField('Categories', choices= [(c.id, c.category)
for c in Categories.query.order_by('category')])

subcategoryfield = StringField('Sub Category Name')

submit = SubmitField('Create SubCategory')

 

Now for the routes.py function which the issue occurs. As you can tell
below, I am passing the category id and category text value to the
SelectField field to create the HTML code. The text value is being displayed
and I want the ID to be retrieved. Then I am planning to update the
SubCategory  table with the category id to establish the relationship for a
new sub_category. 

 

from datetime import datetime

from flask import render_template, flash, redirect, url_for, request

from app import app, db

from app.forms import CategoryForm, SubCategoryForm

from app.models import * # all table model classes 

 

@app.route('/subcategories', methods=['GET', 'POST'])

def subcategories ():

tables = Accounts.query.all()

form = SubCategoryForm()

print (form.categoriesfield.data)

if form.validate_on_submit():

subcategory_value  =
SubCategories(subcategory=form.subcategory.data)

db.session.add(subcategory_value)

db.session.commit()

flash('Congratulations, a sub category was added. {}
{}'.format(subcategory_value, form.categoriesfield.data))

return redirect(url_for('subcategories'))

page = request.args.get('page', 1, type=int)

records  = SubCategories.query.order_by(SubCategories.id).paginate(page,
app.config['POSTS_PER_PAGE'], False)

next_url = url_for('subcategories', page=records.next_num) if
records.has_next else None

prev_url = url_for('subcategories', page=records.prev_num) if
records.has_prev else None

return render_template('subcategories.html', tables = tables,
title='Manage  sub Categories', form=form, records = records.items, next_url
= next_url, prev_url = prev_url)

 

I cannot work how to get the category ID. If I use validate or not on any
fields, it doesn't produce anything in the flash. The page refreshes and
leave the content in the field. The database isn't being  updated with the
new sub_category. All the database relationships are working when I manually
insert the data into the table via SQLIte3.

 

Any thoughts? Have I made myself clear enough?

 

Sean 

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


Re: [Tutor] Blockchain Dev with Python SDK

2019-07-15 Thread mhysnm1964
Matt,

I would suggest you look at the links in the signature for this mailer for
some good tutorials on OOPS. Datacamp also has some good tutorials.

-Original Message-
From: Tutor  On Behalf Of
Matthew Zand
Sent: Monday, 15 July 2019 1:54 AM
To: Tutor@python.org
Subject: [Tutor] Blockchain Dev with Python SDK

Hi Everyone,

I am looking for resources for learning Blockchain development using Python
SDK. I found below tutorial like below but they are too advance:
https://developer.ibm.com/recipes/tutorials/building-transaction-handler-and
-processor-for-hyperledger-sawtooth-with-python-sdk/

Also, I am not familiar with OOP, will that be an issue?

Matt



--
Best,

DC Web Makers 
Coding Bootcamps 
240-200-6131
301-327-8773
___
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] Web framework module for Python.

2019-07-15 Thread mhysnm1964
All,

 

I am currently not sure where to start with my query. 

 

I have a SQLite3 database which currently is being accessed by python code.
I am seeking a simple python module which would support a local web app in
order to update and  insert rows, and run reports  . This web app I am
creating is never going to ever see the public  internet. In fact, it will
be using the localhost address. So I do not require a full blown web server
like apache. If there is a simple python module that can run a web server,
this would be great. If there is a python module that can assist in building
the HTMl, this would be great as well. I am not sure how to link python and
the web HTML page together and haven't found anything that is clear on
addressing my needs.

 

Note: PPySimpleWeb does not fulfil my requirements. As there is
accessibility issues with this module preventing me using it for web. EG: My
screen reader that I use complains about most of the elements which are
generated. I thought of this and tested it. Thus why I have ruled it out.

 

The web pages are going to have:

*   Tables containing report outputs
*   Forms with edit and select tags and buttons/links.
*   Some Headers and basic HTML structure elements.

 

The web page is not going to have any fancy structure or visual effects at
all. I am more than happy to hand-code the HTML/JavaScript. I have had a
quick search and found a range of modules. Most of them indicate they are
for CMS web sites and look far to complex for my needs. 

 

If someone could point myself to a module and possibly a tutorial for the
module. I would be grateful. Hopefully this is not to of a open question.

 

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


[Tutor] pointers or references to variables or sub-sets of variables query.

2019-07-07 Thread mhysnm1964
Hi all.

 

In C, you can use pointers to reference variables, arrays, ETC. In python, I
do not recall anything specifically that refers to such a capability. What I
want to do is:

 

 

I want to create different data structures such as dictionaries which
contain specific  list elements based upon specific keys. The original data
structure could look like:

 

Data = [

  ['2019-01-19','Fred Flintstone',23],

['2019-02-01','Scooby doo', 99]

]

 

The above structure does have 100's of elements. I want to reference
specific lists within the above structure. Using the only method I know how:

 

Category = {'under-50':[data[0]], 'over-50':[data[1]]}

 

If I understand things correctly with Python. The above will copy the value
into the list within the key. Not the memory address of the nested list I am
referencing. I am using a list within the key to permit multiple references
to different nested lists from the original data structure. The end result
of the structure for the dict could look like this (using example, not real
output)

 

 

Category['under-50'] = [ List1 pointer, List22 pointer, List52 pointer]

 

 

I hope the above makes sense. How can this be done? 

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


[Tutor] double nodes being enter into tree structure

2019-06-28 Thread mhysnm1964
Hello All,

 

I am back!!! I forget which movie that comes from. 

 

Anyway, my issue is I am getting the same node being added to the parent node 
of my tree below. I have traced the code and do not understand why. This occurs 
when the loop which calls the addNode function has only looped once. I have had 
the return statement present and commented out with the same result. The length 
of the children list is 2 when it should be 1. 

 

AddNode is using recursive functionality. I suspect the function should be in 
the object itself, but I have it outside for now to work out my logic. I am 
sure there is a logic issue here in the area of OOPS, but I cannot see it. 

 

The aim is two only ever have one word in the children list regardless how many 
times it appears in the original source. Thus if the string has “the brown fox” 
and “the brown car”.  The following has to occur:

*   Children list in the root node will only have the “the” node once.
*   Children list in the “the”  node will only have the “brown” node once.
*   Children in the “brown”  node will have two nodes ‘cow’ and ‘car’.

 

Below is the code:

 

def addNode(words, tree):

# creates the branch of words for the tree

if words:

wordExists = False 

for child in tree.children:

if words[0] == child.name:

wordExists = True

# end if

# end for

if not wordExists:

tree = tree.add(words[0], 0)

addNode(words[1:], tree)

# end if

# note, the below has been uncommented with the same issue.

#return tree 

 

class Node(object):

# I am not 100% sure the purpose of (object) does.

def __init__(self, name, value):

self.parent = None

self.children = []

self.name = name

self.value = value

def add(self, name, value):

node1=Node(name, value)

self.children.append(node1)

node1.parent=self

return node1

 

…. Bunch a code to load the files.

 

for account_no, date, line, amount in records:

tree.children.append(addNode(line.split(), tree))

 

 

 

note: I have tried Allan’s suggestion as shown below and could not get it to 
work. I am currently reading his book on object programming to learn more about 
OOPS. This is how I worked out the recursive function. 

 

def add_child(self, newNode):

if newNode.name != self.name

   self.children.append(newNode)

   newNode.parent = self

 

myNode.add_child(Node(name, value))

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


[Tutor] data structures general query

2019-06-26 Thread mhysnm1964
All,

 

General computer science question for data structures.

When would you use the below structures and why? If you can provide a real
life example on when they would be used in a program  This would be great. I
am not after code, just explanation.

 

Link lists

Double link-lists

Binary trees

What other trees are their other than hierarchical tree (file systems)

 

Link lists I would guess be useful in an index for a database? 

Binary trees useful for searches?

 

Sean

 

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


Re: [Tutor] tree or link-list questions.

2019-06-26 Thread mhysnm1964
Allan,

Once again, thanks for the help. I need to read more on OOPS it is all new
and that was my first attempt. I looked at nesting Dictionaries and it got
quite complex fast with the length of the strings and the path as you have
outlined. I have parked that structure style for now.

All these answers are really helping me to improve my programming knowledge
and narrowing my definition.

The reason why I am using the tree structure like a file system. Is I am
going to attempt to write some code to walk down the tree based upon certain
conditions which I am still working on. Based upon the test conditions will
determine how I move up and down the tree.

The high picture is I am trying to build a dynamic search functionality
based upon keywords in the text. Like a store name or organisation name. The
text has been cleaned up before applying it to the tree based upon text or
punctuation and digits that are not required. Such as receipt  numbers,
postcodes, braces, etc. This is mostly done. I am hoping the tree structure
will help in finishing the search capability. I will only know once I have
tried it. If not, then I will look for another method.

I am enjoying what I am doing because I am learning. I am pushing myself
beyond my normal level of skills. Playing in the dark as I say. LOL 

Sean 
-Original Message-
From: Tutor  On Behalf Of
Alan Gauld via Tutor
Sent: Wednesday, 26 June 2019 2:48 AM
To: tutor@python.org
Subject: Re: [Tutor] tree or link-list questions.

On 25/06/2019 13:24, mhysnm1...@gmail.com wrote:
... the tree I am trying to create is:
> 
> * A single root node
> * A node can only have a single parent.
> * A parent can have multiple children.
> * Each node is unique.

A tree with multiple children (where multiple is more than 2) is entirely
possible but does make the code more complex.

Have you considered a binary tree where items go either left or right
depending on whether they are "greater than" or "less than" (for some
arbitrary definition of greater and less) than the parent node?
This is much simpler to code, and you will find lots of examples online.

-- monospace font needed ---

Root
   the
  brown
 fox
 cow
 car
  yellow
 car
  green
 house
  quick
 brown
fox
   yellow
  flowers

-- end of monospace font ---

The immediate questions you must answer (because only you understand the
requirements) are:
1) How do you know which tree branch to navigate down?
For example you have two car nodes. If searching for car which is the
correct answer? Similarly for fox?

2) How do you know to go down an extra level. For example there are 4 nodes
below 'the' What determines whether a new node sits alongside the existing
nodes or at a level below.

3) Having decided you need to go a level lower how do you know which subnode
to go down? How do you insert the node car into the correct subnode of
the?(either brown or yellow)

It looks like you are doing this based on the textual context.
That means you insertion code has to be stateful since it must remember the
previous word inserted (or have access to the original data). That gets
complicated really quickly.

Similarly, when you try to retrieve the data how do yu know which node to
fetch. For example if I search for fox? Surely you will need to provide the
full 'path' to fox topick the right one? But if you know the path why search
a tree?

In that case nested dictionaries would seem a better solution?
Something like(missing all the quotes!):

data = {
   the: [
  {brown: [
 {fox:[]},
 {cow:[]},
 {car:[]},
 ]},
  {yellow: [
 {car: []}
 ]},
  {green: [
 {house: []}
 ]},
  {quick: [
 {brown: [
 {fox: []}
 ]},
 ]},
   ]},
   {yellow: [
  {flowers: []}
  ]}
   ]}

Then you can access the appropriate path:

myFox = data[the][quick][brown][fox]

Just a thought. Since I don't really know how you intend to use this I'm not
sure if that would work for you.


--
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 maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] tree or link-list questions.

2019-06-25 Thread mhysnm1964
All,

 

Windows 10, python 3.6.

 

I am trying to create a data structure. I might be using the wrong term here, 
but I am calling it a hierarchical tree. The properties of the tree I am trying 
to create is:

*   A single root node
*   A node can only have a single parent.
*   A parent can have multiple children.
*   Each node is unique.

This is also my first attempt in doing Object coding ever. Thus the terminology 
I am not going to use correct. 

 

Data structure:

records = [‘the brown fox',

‘the brown cow’,

‘the brown car’,

‘the yellow car’,

‘the green house’,

‘yellow flowers’,

‘the quick brown fox’]



What I am trying to do:

I want to create a tree structure that stores the first word under the root, 
the 2nd word under the first word as a child, ETC. the word ‘the’ for example 
can only occur once as a node. Below is an example of the tree structure I am 
trying to achieve:

 

Root

Root / the

Root / the / brown

Root / the / brown / fox

Root / the / brown / cow

Root / the / brown / car 

Root / the / yellow

Root / the / yellow / car

Root / the / green 

Root / the / green / house

Root / the / quick 

Root / the / quick / brown 

Root / the / quick / brown / fox

Root / yellow

Root / yellow / flowers

 

The code I have used to break up the words and build a dictionary to identify 
the unique words plus the hit count:

 

nar_dict = {}

for text  in records:

words = text.split()

l = len(words)

for i in range(1, l+1):

# only method I have found to correctly join the words.

tmp_list = words[:i]

key = ' '.join(tmp_list)

nar_dict[key] = 0

 

# perform the searches using dictionary key and count the number of hits.

for key, item  in nar_dict.items():

print (key, item)

for line  in records:

if  line.startswith(key):

nar_dict[key] += 1

 

The result  is (note, the output below is using a different data set):

 

{'the': 5, 'the brown': 3, 'the brown cow': 2, 'the brown car': 1, 'the 
yellow': 1, 'the yellow car': 1, 'yellow': 2, 'yellow house': 1, 'the quick': 
1, 'the quick fox': 1, 'yellow flowers': 1}

 

The code below works fine if I want to have a single parent and a single child. 
The init definition I have used an empty array to store the children. I am not 
sure how I can reference the new node back to the parent node. I have looked at 
some packages like anytree and I got completely lost with the code. I have 
looked for examples on the net and did not find anything I could understand. 
Here is my code thus far with the children as a list.

# Value = hit counter 

# Name = node name 

 

class Node:

def __init__(self, name, value):

self.parent = None

self.child = []

self.name = name

self.value = value

def add_child(self, name, value):

# Compare the new value with the parent node

if self.name:

if name != self.name:

if  self.child is None:

self.parent = self.child

self.child = Node(name, value)

else:

self.child.add_child(name, value)

else:

self.name  = name

self.value = value

# Print the tree

def PrintTree(self):

if self.child:

self.child.PrintTree()

print( self.name, self.value),

I want to be able to do the following syntax:

 

Node.children[-1]value

Child = Node.children[0].children[0]

Parent = child.parent.name 

 

 

I know I will have to write a search and walk functionality. 

 

Any comments and tips on how to fix the parent issue. I would be grateful. 
Hopefully this is not to open ended. Done my best to be very specific. 

 

Sean – Open ended question master 

 

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


Re: [Tutor] collections and mappings

2019-06-21 Thread mhysnm1964
Allan,

I think I understand, but that type of maths I have not touched in 40 years. 
Thus why I am not getting the concept.  It was an open question as I had no 
clue what it was and I should have asked. Lets park the question for now and I 
will read the references. As I am not sure if this will help my program that I 
am struggling with. 


-Original Message-
From: Tutor  On Behalf Of Alan 
Gauld via Tutor
Sent: Friday, 21 June 2019 6:11 PM
To: tutor@python.org
Subject: Re: [Tutor] collections and mappings

On 21/06/2019 01:01, mhysnm1...@gmail.com wrote:

> I have reviewed the collection module and do not understand mappings. 
> I have seen this in other languages and have never got the concept. 
> Can someone explain this at a very high level.

OK. You are a master of the open ended question so I'm not sure what aspect you 
don't understand.
But I'll start at the beginning and take it as far as Python.
But you are opening a vary large can of worms...

Mappings, in programming terms, are related to a mathematical concept.
See Wikipedia for a more detailed account of math mapping.

In simplistic terms a mapping comprises two sets of data, one an input the 
other an output. The map is the set of relationships between input and output. 
Thus given input of {a, b, c} and output of {1,2,3,4,5} We might have any of 
several mappings between these. A simple 1:1 mapping might be

{a,1}, {b,2}, {c,3}

But we can have 1:N or N:1 or N:M mappings too:

{a,1,2} {b,3} {c,4} - a 1:N mapping
{a,1} {b,1}, {c,3}  - an N:1 mapping
{a,1,2} {b,2},{c,1,3}   - a N:M mapping

Note that the mapping does not have to include all of the output elements.

The mapping may be arbitrary, as above or it may be defined as a function:

{a, f(a)} {b,f(b)} {c,f(c)}

Or even as a set of functions...

In programming, and particularly in Python, this tends to be represented as a 
dictionary (or sometimes a class).

So the mappings above could be shown as:

input = ['a','b','c']
output = [1,2,3]
1_1 = {'a':1,'b':2,'c':3}
1_N = {'a':(1,2)'b':(1,),'c':(3,)}
N_1 = {'a':1,'b':1,'c':3}
N_M = {'a':(1,2),'b':(2,),'c:(1,3)}

def f(x): return x*2
1_f = {'a':f('a'),'b':f('b'),'c':f('c')}

List comprehensions and generator expressions are also commonly used to create 
mappings, especially the functional sort.

I have no idea if the addressed any of your questions but if not please ask 
again, but with something more specific.

PS. To the real mathematicians on the list. My math is very rusty, if I've made 
any major gaffes please feel free to correct/amend my scribblings.

--
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 maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] collections and mappings

2019-06-21 Thread mhysnm1964
All,

 

I have reviewed the collection module and do not understand mappings. I have
seen this in other languages and have never got the concept. Can someone
explain this at a very high level.  

 

 

 

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


Re: [Tutor] word printing issue

2019-06-21 Thread mhysnm1964
Thanks, so much to learn.

-Original Message-
From: Tutor  On Behalf Of
Alan Gauld via Tutor
Sent: Friday, 21 June 2019 2:26 AM
To: tutor@python.org
Subject: Re: [Tutor] word printing issue

On 20/06/2019 11:44, mhysnm1...@gmail.com wrote:

> I have a list of strings that I want to break them into separate 
> words, and a combination of words then store them into a list. Example 
> below of a
> string:

> "Hello Python team".
> The data structure:
> [ ['Hello'],
> ['Hello', 'Python'],
> ['Hello', 'Python', 'team'],
> ]'Python'],
> ]'Python', 'team'],
> ['team'] ]
> 
>  
> 
> I want to know if there is a better method in doing this without the 
> requirement of a module.

Modules are there to be used...

Here is one with itertools from the standard library that gets close:

input = "hello Python team".split()
result = []
for n in range(len(input):
   result += [item for item in it.combinations(input,n+1)]

If you really want to do it from scratch then Google combinations algorithm,
or look on wikipedia.

--
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 maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] word printing issue

2019-06-20 Thread mhysnm1964
All,

 

I have a list of strings that I want to break them into separate words, and
a combination of words then store them into a list. Example below of a
string:

 

"Hello Python team".

 

The data structure:

 

[ ['Hello'],

['Hello', 'Python'],

['Hello', 'Python', 'team'],

]'Python'],

]'Python', 'team'],

['team'] ]

 

I want to know if there is a better method in doing this without the
requirement of a module. Eventually I want to count the number of hits in a
list of strings based upon the word combinations regardless where they are
located in the string. This last part is something I am struggling with to
get results that are correct. I have stored them in a dictionary and get
unexpected totals. Was thinking of using collection and still working on it.
If the above could be improved. I would be grateful.

 

Sean 

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


Re: [Tutor] deleting elements out of a list.

2019-06-16 Thread mhysnm1964
Allan,

 

I will respond to your reply in the other thread. As I want to respond to 
everyone in one email. Thanks for the clarification on the questions or 
information I did not provide. 101 troubleshooting assistance. Do not assume 
others know what you know. 

 

-Original Message-
From: Tutor  On Behalf Of Alan 
Gauld via Tutor
Sent: Sunday, 16 June 2019 7:25 AM
To: tutor@python.org
Subject: Re: [Tutor] deleting elements out of a list.

 

On 15/06/2019 05:51,   mhysnm1...@gmail.com wrote:

 

Caveat: I'm picking this up late in the day and only had a cursory look at it, 
so may be missing some critical insight...

 

> I have a list of x number of elements. Some of the elements are have 

> similar words in them. For example:

 

Define "similar".

It implies not identical. What is different? What makes them similar? Every 
time you introduce vague inequalities you imply the need for some kind of 
intelligent function that removes the ambiguity and vagueness. it  definitively 
says that these two items are similar or not similar.

 

Can you write such a function? If so the problem should become relatively 
simple.

 

 

> Dog food Pal

> Dog Food Pal qx1323

> Cat food kitty

> Absolute cleaning inv123

> Absolute Domestic cleaning inv 222

> Absolute d 

> Fitness first 02/19

> Fitness first

> 

> I wish to remove duplicates. 

 

So what would the output look like if the above is the input?

My guess of what you want is:

 

qx1323

Cat kitty

Domestic

d 

02/19

 

Is that right?

Or is my idea of similar and duplicate different to yours?

 

> I could use the collection.Count method. This fails due to the strings 

> are not unique, only some of the words are.

 

Sorry, I can't understand that. It makes no sense to me.

You need to define strings and words in this context

 

> description = load_files() # returns a list

 

A list of what? characters, words, lines?

 

> for text in description:

> words = text.split()

> for i in enumerate(words):

> Word = ' '.join(words[:i])

 

This is weird. enumerate returns tuples which you assign to i.

But then you use i in a slice opertion. But slice expects an integer.

 

 

> print (word)

> answer = input('Keep word?')

> if answer == 'n':

> continue 

> for i, v in enumerate(description):

> if word in description[i]:

> description.pop[i]

 

Without any clue what the description data looks like we can't really decipher 
what the code above does.

 

> description list will cause a error. If I copy the description list 

> into a new list. And use the new list for the outer loop. I will 

> receive multiple occurrences of the same text.

 

I'm not sure thats true but it denends on what description looks like.

 

> 

> description = load_files() # returns a list

> 

> search_txt = description.copy()

> 

> for text in search_txt:

> words = text.split()

> for i in enumerate(words):

> Word = ' '.join(words[:i])

> print (word)

> answer = input('Keep word (ynq)?')

> if answer == 'n':

> continue 

> elif answer = 'q':

> break

> 

> for i, v in enumerate(description):

> if word in description[i]:

> description.pop[i]

 

The usual way to remove things from a list is to create a new list using a 
filter

 

newlist = filter(test_function, oldlist)

 

or a list comprehension

 

newlist = [item for item in oldlist if test_function(item)]

 

Which brings us back to the beginning. Can you write a test function that 
unambiguously defines what needs to be removed?

 

 

--

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 maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Follow-up on my removing elements from lists question.

2019-06-15 Thread mhysnm1964
This is a follow-up on my previous question for removing elements. Below is
the code I am currently using. I am removing the elements at the end of the
outer loop. The data structure goes along this:

 

[ 

  ['123123',[2019-2-18', 'transaction text', 'amount'],

v  ['123123',[2019-2-18', 'transaction text', 'amount'],

  ['123123',[2019-2-18', 'transaction text', 'amount']

]

 

The 2nd column where the transaction text I am modifying the content and
using the end result of the built-up words as the string match as you will
see in the code. This is all working fine. The last loop in the code I am
trying to delete the elements in reverse order. This doesn't work. The
length of the list reduces by 1. When it should have reduced by 42. Is the
logic wrong? This is in Python 3.6 under windows 10.

 

unknown_transactions.sort(key=lambda x: x[2])

while True:

# re-initialise each time the current transaction text has been processed.

for row in unknown_transactions:

# remove common words from transactions which are not required. Such
as 'WITHDRAWAL' and 'DEPOSIT'.

line = regex_transaction(row[2])

# If the common words are not found, return a null and do not modify
the transaction description.

if line != '': # not a null string

# now find unique string and add it to the compare_transactions
list object.

words = line.split()

print ('List length:', len(unknown_transactions))

word = ''

for i, v in enumerate(words, start=1):

word = ' '.join(words[:i])

print (word)

answer = input('Use  word y, otherwise any other key
continues...')

if answer != 'y':

continue

# end if 

# end for 

# now loop through the unknown transactions and copy to
transaction dictionary

delete_transactions = []

for e, v in enumerate (unknown_transactions):

if word in unknown_transactions[e][2]:

if not word  in transaction:

transaction[word] = unknown_transactions

else:

transaction[word].append(unknown_transactions)

# end if  

delete_transactions.append (e)

# end if 

# end for 

print ('number of elements to remove:',
len(delete_transactions))

for del_element in reversed(delete_transactions):

unknown_transactions.pop(del_element)

# end if 

# end for 

if len(unknown_transactions) == 0:

break

# end if 

# end while

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


[Tutor] Differences between while and for

2019-06-15 Thread mhysnm1964
All,

 

In C, Perl and other languages. While only uses a conditional statement and
for uses an iteration. In python while and for seems to be the same and I
cannot see the difference. Python does not have an until (do while) where
the test is done at the end of the loop. Permitting a once through the loop
block. Am I correct or is there a difference and if so what is it?

 

Why doesn't Python have an until statement?

 

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


[Tutor] deleting elements out of a list.

2019-06-15 Thread mhysnm1964
All,

 

I am not sure how to tackle this issue. I am using Windows 10 and Python 3.6
from Activestate. 

 

I have a list of x number of elements. Some of the elements are have similar
words in them. For example:

 

Dog food Pal

Dog Food Pal qx1323 

Cat food kitty

Absolute cleaning inv123

Absolute Domestic cleaning inv 222

Absolute d 

Fitness first 02/19

Fitness first

 

I wish to remove duplicates. I could use the collection.Count method. This
fails due to the strings are not unique, only some of the words are. My
thinking and is only rough sudo code as I am not sure how to do this and
wish to learn and not sure how to do without causing gtraceback errors. I
want to delete the match pattern from the list of strings. Below is my
attempt and I hope this makes sense. 

 

 

description = load_files() # returns a list

for text in description:

words = text.split()

for i in enumerate(words):

Word = ' '.join(words[:i])

print (word)

answer = input('Keep word?')

if answer == 'n':

continue 

for i, v in enumerate(description):

if word in description[i]:

description.pop[i]



 

The initial issues I see with the above is the popping of an element from
description list will cause a error. If I copy the description list into a
new list. And use the new list for the outer loop. I will receive multiple
occurrences of the same text. This could be addressed by a if test. But I am
wondering if there is a better method. 2nd code example:

 

description = load_files() # returns a list

search_txt = description.copy() # I have not verify if this is the right
syntax for the copy method.]

for text in search_txt:

words = text.split()

for i in enumerate(words):

Word = ' '.join(words[:i])

print (word)

answer = input('Keep word (ynq)?')

if answer == 'n':

continue 

elif answer = 'q':

break

for i, v in enumerate(description):

if word in description[i]:

description.pop[i]



 

Any improvements?

 

Sean 

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


[Tutor] regular expression query

2019-06-08 Thread mhysnm1964
Hello all,

 

Windows 10 OS, Python 3.6

 

I have a couple of  queries  in relation to extracting content using regular
expressions. I understand the pattern chars (.?*+), Meta-chars \d, \D, \W,
\W and so on. The class structure [.]. The group I believe I understand (.).
The repeat feature {m,n}. the difference between the methods match, search,
findall, sub and ETC. The challenge I am finding is getting a pattern to
extract specific word(s). Trying to identify the best method to use and how
to use the \1 when using forward and backward search pattern (Hoping I am
using the right term). Basically I am trying to extract specific phrases or
digits to place in a dictionary within categories. Thus if "ROYaL_BANK
123123123" is found, it is placed in a category called transfer funds. Other
might be a store name which likewise is placed in the store category. 

 

Note, I have found a logic error with "ROYAL_BANK 123123123", but that isn't
a concern. The extraction of the text is.

 

Line examples:

 

Royal_bank M-BANKING PAYMENT TRANSFER 123456 to 9922992299

Royal_bank M-BANKING PAYMENT TRANSFER 123456 FROM 9922992299

PAYMENT TO SARWARS-123123123

ROYAL_BANK INTERNET BANKING BPAY Kangaroo Store {123123123}result =
re.sub(r'ROYAL_BANK INTERNET BANKING FUNDS TFER TRANSFER \d+ TO ',
'ROYAL_BANK ', line)

r'ROYAL_BANK INTERNET BANKING TRANSFER Mouth in foot

EFTPOS Amazon

PAY/SALARY FROM foo bar 123123123

PAYMENT TO Tax Man  666

 

And other similar structures. Below is the function I am currently using.
Not sure if the sub, match or search is going to be the best method. The
reason why I am using a sub is to delete the unwanted text. The
searchmatch/findall  could do the same if I use a group. Also I have not
used any tests in the below and logically I think I should. As the code will
override the results if not found in the later tests. If there is a more
elegant  way to do it then having:

 

If line.startswith('text string to match'):

Regular expression 

el If line.startswith('text string to match'):

regular expression

return result 

 

I would like to know. The different regular expressions I have used are:

 

# this sometimes matches and sometimes does not. I want all the text up to
the from or to, to be replaced with "ROYAL_BANK". Ending up with ROYAL_BANK
123123123

result= re.sub(r'ROYAL_BANK M-BANKING PAYMENT TRANSFER \d+ (TO|FROM) ',
'ROYAL_BANK ', line)

 

# the below  returns from STARWARS and it shouldn't. I should just get
STARWARS.

result = re.match(r'PAYMENT TO (SARWARS)-\d+ ', line)

 

# the below should (doesn't work the last time I tested it) should return
the words between the (.)

result = re.match(r'ROYAL_BANK INTERNET BANKING BPAY (.*) [{].*$', '\1',
line)

 

# the below patterns should remove the text at the beginning of the string

result = re.sub(r'ROYAL_BANK INTERNET BANKING FUNDS TFER TRANSFER \d+ TO
', 'ROYAL_BANK ', line)

result = re.sub(r'ROYAL_BANK INTERNET BANKING TRANSFER ', '', line)

result = re.sub(r'EFTPOS ', '', line)

 

# The below does not work and I am trying to use the back or forward search
feature. Is this syntax wrong or the pattern wrong? I cannot work it out
from the information I have read.

 result = re.sub(r'PAY/SALARY FROM (*.) \d+$', '\1', line)

result = re.sub(r'PAYMENT TO (*.) \d+', '\1', line)

 

Sean 

 

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


Re: [Tutor] Interactive editing of variables.

2019-06-02 Thread mhysnm1964
Mike, Allan and Matt,

Thanks for the information and help. I will check out Mike's code and have a
play. Allan you provided some good resources and based upon what Matt stated
before and using Easegui. This is not a path I can use. Thanks for the help
anyway.

Matt, a shame. I am not sure what can be done in this area for TK, as  it is
open source other than someone with the knowledge introducing accessibility.
At the level required to provide the required accessibility framework for
different platforms for the default UI controls , is far beyond my skill
level.

I believe later versions of QT  support iaccess2 framework which is an
accessibility framework.

I know from a legal point of view. If a developer or company built a product
based upon the GUI environments and sold it to the USA Government or to the
public. They are opening themselves to potential legal action. This concept
applies in other countries. Accessibility is on a up swing and Microsoft,
Apple, Google, Cisco and others are focusing on this area due to the change
in the landscape. 

What I do not know, how this applies to open source. If there is no
commercial transaction. Then this is the area I am unsure if any of the laws
I am indirectly referring to impact. Anyway, this is getting off scope. Just
highlighting so people are aware. Accessibility is a part of best practice
for UX, UI and development.

-Original Message-
From: Tutor  On Behalf Of
Mats Wichmann
Sent: Sunday, 2 June 2019 5:24 AM
To: tutor@python.org
Subject: Re: [Tutor] Interactive editing of variables.


>> The issue I have with a lot of GUI programs built for Python they 
>> generally fail in the accessibility department for a screen reader.
> 
> I can't help there I have nearly zero experience of using 
> accessibility tools. But I'd expect any GUI toolkit to work with the 
> standard OS tools. After all they are ultimately all built using the 
> underlying primitive GUI API

On the gui front,

tk developers make no bones about tk not having been built with
accessibility considerations  tk (and thus tkinter which is just the Python
binding to tk),  is not going to work with a screen reader.

wxPython is probably the best choice, it explicitly has support (although
sadly only for Windows):

https://docs.wxpython.org/wx.Accessible.html

possibly some of the other toolkits do - I wouldn't rule out Qt either, but
haven't any experience.

___
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] Interactive editing of variables.

2019-06-02 Thread mhysnm1964
Allan,

That is what I have done before I went to bed. Installed ActiveState and
using Python 3.6 as that is the release build they have up on their site.





-Original Message-
From: Tutor  On Behalf Of
Alan Gauld via Tutor
Sent: Sunday, 2 June 2019 5:01 AM
To: tutor@python.org
Subject: Re: [Tutor] Interactive editing of variables.

On 01/06/2019 09:52, mhysnm1...@gmail.com wrote:

> the underlying graphic library. Win32 could work if I could load it. 
> Since then I could use standard windows objects.

If you are running windows then you can access the Win32 DLLs via ctypes.

The win32 package should also be easily installable as a binary.
If not try using the ActiveState distribution of python because it bundles
all the windows tools in the installer. Personally I always use ActiveState
pyton for my Windows boxes.


--
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 maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Interactive editing of variables.

2019-06-01 Thread mhysnm1964
As I thought. Easygui is not accessible at all with a screen reader due to
the underlying graphic library. Win32 could work if I could load it. Since
then I could use standard windows objects.

Note, I didn't see anything in the quick scan I did ion the API. 


Very frustrating and disappointing. 
-Original Message-
From: Tutor  On Behalf Of
Alan Gauld via Tutor
Sent: Saturday, 1 June 2019 4:50 PM
To: tutor@python.org
Subject: Re: [Tutor] Interactive editing of variables.

On 01/06/2019 03:53, mhysnm1...@gmail.com wrote:

> I have no clue on how to achieve what I want to do and the code I have 
> creates an hash. As shown below:

Thats because what you want is not a standard feature of CLI apps.
You will need to do one of the following(in order of easiness):
1) Use a GUI - it then becomes a trivial matter
2) Use a pseudo GUI like curses to provide cursor control
3) Find a module that already does what you need
   (maybe readline can be made to work?)
4) Write a function yourself using screen primitives
   that manage the cursor


> for row in description:
> text = description_rejex(row)
> if text[0] not in narration: 
> Result = input(text[0])
> narration[result] = text

The standard tools allow you to input a new value and overwrite the existing
one. But there is no simple way to interactively modify an existing value
(and of course you would need to convert it to/from a string for that to be
possible)

> I have had a look and cannot find an example where I can interactively 
> edit a content of a variable at the command line. I do not want to use 
> GUI at all.

A GUI makes this a trivial problem. Simply display an edit control and
insert the current value as a string. Allow the user to modify it and when
done read the new value back. If you don't want to use a GUI you need to
provide GUI like controls yourself, either through an existing module or by
writing one. Something like easygui would be eminently suitable. But even
vanilla Tkinter is almost trivial.

The curses library will do it but that is not standard on Windows and I've
heard varying reports of how well it works there.

The readline library allows basic editing of the commands line but I'm not
sure how you would insert your variable into the command line initially...

For windows there are a couple of modules available that provide low level
cursor control and character input/output, so you could use one of those to
write such a function.

And if you search hard enough you should find that somebody, somewhere has
already done the work for you. But I don't know where...

--
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 maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Interactive editing of variables.

2019-06-01 Thread mhysnm1964
Allen,

As I am using Python 3.7 under windows. I have tried to use the win32gui,
and Tkinter. Both generate the below errors and I cannot identify a module
release to support the version of Python I am using.

C:\Users\mhysn>pip install Tkinter
graphic 948 Collecting Tkinter 
 

  ERROR: Could not find a version that satisfies the requirement Tkinter
(from versions: none)  graphic 948 ERROR: No
matching distribution
found for Tkinter


I gave up on the cursers due to the above issues and looking at example
code. It was far to complex to achieve the simple thing I wanted too do.
Easygui module has installed and I will look for examples.

The issue I have with a lot of GUI programs built for Python they generally
fail in the accessibility department for a screen reader. Most open source
software which is multi-platform supported fail in this department. I will
check out the module and see. This is a major issue for software developers
not considering all users and there is legal requirements here. Sorry, I am
falling on to my band wagon of in-accessible or non-inclusive design
products which is my passion.



-Original Message-
From: Tutor  On Behalf Of
Alan Gauld via Tutor
Sent: Saturday, 1 June 2019 4:50 PM
To: tutor@python.org
Subject: Re: [Tutor] Interactive editing of variables.

On 01/06/2019 03:53, mhysnm1...@gmail.com wrote:

> I have no clue on how to achieve what I want to do and the code I have 
> creates an hash. As shown below:

Thats because what you want is not a standard feature of CLI apps.
You will need to do one of the following(in order of easiness):
1) Use a GUI - it then becomes a trivial matter
2) Use a pseudo GUI like curses to provide cursor control
3) Find a module that already does what you need
   (maybe readline can be made to work?)
4) Write a function yourself using screen primitives
   that manage the cursor


> for row in description:
> text = description_rejex(row)
> if text[0] not in narration: 
> Result = input(text[0])
> narration[result] = text

The standard tools allow you to input a new value and overwrite the existing
one. But there is no simple way to interactively modify an existing value
(and of course you would need to convert it to/from a string for that to be
possible)

> I have had a look and cannot find an example where I can interactively 
> edit a content of a variable at the command line. I do not want to use 
> GUI at all.

A GUI makes this a trivial problem. Simply display an edit control and
insert the current value as a string. Allow the user to modify it and when
done read the new value back. If you don't want to use a GUI you need to
provide GUI like controls yourself, either through an existing module or by
writing one. Something like easygui would be eminently suitable. But even
vanilla Tkinter is almost trivial.

The curses library will do it but that is not standard on Windows and I've
heard varying reports of how well it works there.

The readline library allows basic editing of the commands line but I'm not
sure how you would insert your variable into the command line initially...

For windows there are a couple of modules available that provide low level
cursor control and character input/output, so you could use one of those to
write such a function.

And if you search hard enough you should find that somebody, somewhere has
already done the work for you. But I don't know where...

--
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 maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Interactive editing of variables.

2019-06-01 Thread mhysnm1964
Hello all,

 

Python 3.7, windows 10.

 

I have no clue on how to achieve what I want to do and the code I have
creates an hash. As shown below:

 

for row in description:

text = description_rejex(row) # applies a regular expression test
function to remove text. Returns a list.

if text[0] not in narration: # Checks a hash to see if the first element
is in the dictionary.

Result = input(text[0]) # the issue, I want to modify some of the
results manually before going into the dictionary.

narration[result] = text

 

I have had a look and cannot find an example where I can interactively edit
a content of a variable at the command line. I do not want to use GUI at
all. As this is a simple program only requiring CLI. I have no problems
showing the prompt, but cannot insert text into the edit (input) area. Any
ideas?

 

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


[Tutor] regular expressions query

2019-05-24 Thread mhysnm1964
All,

 

Below I am just providing the example of what I want to achieve, not the
original strings that I will be using the regular expression against.

 

The original strings could have:

 

"Hello world"

"hello  World everyone"

"hello everyone"

"hello   world and friends"

 

I have a string which is "hello world" which I want to identify by using
regular expression how many times:

*   "hello" occurs on its own.
*   "Hello world" occurs in the list of strings regardless of the number
of white spaces.

 

Splitting the string into an array ['hello', 'world'] and then re-joining it
together and using a loop to move through the strings does not provide the
information I want. So I was wondering if this is possible via regular
expressions matching?

 

Modifying the original string is one option. But I was wondering if this
could be done?

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


Re: [Tutor] Finding unique strings.

2019-05-04 Thread mhysnm1964
Mark and all,

Thanks for the link to the different dictionaries. The book which was
recommended I have already got and read which doesn't answer my question.
The structure of the CSV file is:

Account no, date, transaction description (what I am trying to build unique
keys from), credit, debit, serial and transaction type.

I have already loaded the cSV file into a list. Thus why I did not show any
code.  I will review the page provided and if I have more questions which
are going to e more than likely. I will come back using the same bat
channel.

Sean 

-Original Message-
From: Tutor  On Behalf Of
Mark Lawrence
Sent: Saturday, 4 May 2019 7:35 AM
To: tutor@python.org
Subject: Re: [Tutor] Finding unique strings.

On 03/05/2019 13:07, mhysnm1...@gmail.com wrote:
> All,
> 
> I have a list of strings which has been downloaded from my bank. I am 
> trying to build a program to find the unique string patterns which I 
> want to use with a dictionary. So I can group the different 
> transactions together. Below are example unique strings which I have
manually extracted from the data.
> Everything after the example text is different. I cannot show the full 
> data due to privacy.
> 
> WITHDRAWAL AT HANDYBANK
> 
> PAYMENT BY AUTHORITY
> 
> WITHDRAWAL BY EFTPOS
> 
> WITHDRAWAL MOBILE
> 
> DEPOSIT  ACCESSPAY
> 
> Note: Some of the entries, have an store name contained in the string 
> towards the end. For example:
> 
> WITHDRAWAL BY EFTPOS 0304479 KMART 1075   CASTLE HILL 24/09
> 
> Thus I want to extract the KMART as part of the unique key. As the 
> shown example transaction always has a number. I was going to use a 
> test condition for the above to test for the number. Then the next 
> word would be added to the string for the key.
> I tried to use dictionaries and managed to get unique first words. But 
> got stuck at this point and could not work out how to build a unique 
> keyword with multiple words. I hope someone can help.
> 
> Sean
> 

Please check out
https://docs.python.org/3/library/collections.html#collections.defaultdict
as I think it's right up your street.  Examples are given at the link  :)

--
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

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


[Tutor] Finding unique strings.

2019-05-03 Thread mhysnm1964
All,

 

I have a list of strings which has been downloaded from my bank. I am trying
to build a program to find the unique string patterns which I want to use
with a dictionary. So I can group the different transactions together. Below
are example unique strings which I have manually extracted from the data.
Everything after the example text is different. I cannot show the full data
due to privacy.

 

WITHDRAWAL AT HANDYBANK 

PAYMENT BY AUTHORITY

WITHDRAWAL BY EFTPOS

WITHDRAWAL MOBILE

DEPOSIT  ACCESSPAY

 

Note: Some of the entries, have an store name contained in the string
towards the end. For example:

 

WITHDRAWAL BY EFTPOS 0304479 KMART 1075   CASTLE HILL 24/09

 

Thus I want to extract the KMART as part of the unique key. As the shown
example transaction always has a number. I was going to use a test condition
for the above to test for the number. Then the next word would be added to
the string for the key.

 

I tried to use dictionaries and managed to get unique first words. But got
stuck at this point and could not work out how to build a unique keyword
with multiple words. I hope someone can help.

 

 

Sean 

  

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


Re: [Tutor] How to get Selenium to wait for page load

2019-02-01 Thread mhysnm1964
Everyone,

 

Thanks for the pointers. 

 

From: George Fischhof  
Sent: Saturday, 2 February 2019 2:42 AM
To: mhysnm1...@gmail.com
Cc: tutor@python.org
Subject: Re: [Tutor] How to get Selenium to wait for page load

 

 

mailto:mhysnm1...@gmail.com> > ezt írta (időpont: 2019. 
jan. 31., Cs 12:07):

Hi all,



I have found an excellent article on identifying stale elements. The issue
is when I try and use their example code. I get a failure where for_wait is
not defined.

http://www.obeythetestinggoat.com/how-to-get-selenium-to-wait-for-page-load- 

 
after-a-click.html



Traceback (most recent call last): 
File "", line 5, in  
NameError: name 'wait_for' is not defined 
>>>



When I look through the examples. I only find one reference for the above
function. But it doesn't look correct and I am confused. The author
indicates the definition of the function is half way up the page. As I
cannot see, this reference doesn't help. So can someone help and provide the
definition for this method/function?



The code which generated the error:



rows = []

pageNav = browser.find_element_by_id("center-5")

curr_page = pageNav.find_element_by_css_selector('span
.pageNumberElement').text

prev_page = ""

while prev_page in curr_page:

#wait_for(link_has_gone_stale):

prev_page = curr_page  

rows.extend(tableNavigation (pageNav))

if wait_for(link_has_gone_stale):

pageNav = browser.find_element_by_id("center-5")

curr_page = pageNav.find_element_by_css_selector('span
.pageNumberElement').text

if prev_page == curr_page:

print ("Page no has not changed:",curr_page)

else:

prev_page = curr_page





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

 

 

Hi, 

 

Try splinter package.

That is an abstraction layer over selenium, and exactly does similar things.

 

__george__

 

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


[Tutor] How to get Selenium to wait for page load

2019-01-31 Thread mhysnm1964
Hi all,

 

I have found an excellent article on identifying stale elements. The issue
is when I try and use their example code. I get a failure where for_wait is
not defined.

http://www.obeythetestinggoat.com/how-to-get-selenium-to-wait-for-page-load-
after-a-click.html

 

Traceback (most recent call last): 
File "", line 5, in  
NameError: name 'wait_for' is not defined 
>>>

 

When I look through the examples. I only find one reference for the above
function. But it doesn't look correct and I am confused. The author
indicates the definition of the function is half way up the page. As I
cannot see, this reference doesn't help. So can someone help and provide the
definition for this method/function?

 

The code which generated the error:

 

rows = []

pageNav = browser.find_element_by_id("center-5")

curr_page = pageNav.find_element_by_css_selector('span
.pageNumberElement').text

prev_page = ""

while prev_page in curr_page:

#wait_for(link_has_gone_stale):

prev_page = curr_page  

rows.extend(tableNavigation (pageNav))

if wait_for(link_has_gone_stale):

pageNav = browser.find_element_by_id("center-5")

curr_page = pageNav.find_element_by_css_selector('span
.pageNumberElement').text

if prev_page == curr_page:

print ("Page no has not changed:",curr_page)

else:

prev_page = curr_page

 

 

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


Re: [Tutor] Web scraping using selenium and navigating nested dictionaries / lists.

2019-01-27 Thread mhysnm1964
Peter,

I am aware that I am avoiding functions that can make my life easier. But I
want to learn some of this data structure navigation concepts to improve my
skills in programming. What you have provided I will review in depth and
have a play with.

A big thanks.


-Original Message-
From: Tutor  On Behalf Of
Peter Otten
Sent: Sunday, 27 January 2019 10:13 PM
To: tutor@python.org
Subject: Re: [Tutor] Web scraping using selenium and navigating nested
dictionaries / lists.

mhysnm1...@gmail.com wrote:

> All,
> 
>  
> 
> Goal of new project.
> 
> I want to scrape all my books from Audible.com that I have purchased.
> Eventually I want to export this as a CSV file or maybe Json. I have 
> not got that far yet. The reasoning behind this is to  learn selenium  
> for my work and get the list of books I have purchased. Killing two 
> birds with one stone
> here. The work focus is to see if selenium   can automate some of the
> testing I have to do and collect useful information from the web page 
> for my reports. This part of the goal is in the future. As I need to 
> build my python skills up.
> 
>  
> 
> Thus far, I have been successful in logging into Audible and showing 
> the library of books. I am able to store the table of books and want 
> to use BeautifulSoup to extract the relevant information. Information 
> I will want from the table is:
> 
> * Author
> * Title
> * Date purchased
> * Length
> * Is the book in a series (there is a link for this)
> * Link to the page storing the publish details.
> * Download link
> 
> Hopefully this has given you enough information on what I am trying to 
> achieve at this stage. AS I learn more about what I am doing, I am 
> adding possible extra's tasks. Such as verifying if I have the book 
> already download via itunes.
> 
>  
> 
> Learning goals:
> 
> Using the BeautifulSoup  structure that I have extracted from the page 
> source for the table. I want to navigate the tree structure. 
> BeautifulSoup provides children, siblings and parents methods. This is 
> where I get stuck with programming logic. BeautifulSoup does provide 
> find_all method plus selectors which I do not want to use for this 
> exercise. As I want to learn how to walk a tree starting at the root 
> and visiting each node of the tree.

I think you make your life harder than necessary if you avoid the tools
provided by the library you are using.

> Then I can look at the attributes for the tag as I go. I believe I 
> have to set up a recursive loop or function call. Not sure on how to 
> do this. Pseudo code:
> 
>  
> 
> Build table structure
> 
> Start at the root node.
> 
> Check to see if there is any children.
> 
> Pass first child to function.
> 
> Print attributes for tag at this level
> 
> In function, check for any sibling nodes.
> 
> If exist, call function again
> 
> If no siblings, then start at first sibling and get its child.
> 
>  
> 
> This is where I get struck. Each sibling can have children and they 
> can have siblings. So how do I ensure I visit each node in the tree?

The problem with your description is that siblings do not matter. Just

- process root
- iterate over its children and call the function recursively with every
  child as the new root.

To make the function more useful you can pass a function instead of hard-
coding what you want to do with the elements. Given

def process_elements(elem, do_stuff):
do_stuff(elem)
for child in elem.children:
process_elements(child, do_stuff)

you can print all elements with

soup = BeautifulSoup(...)
process_elements(soup, print)

and

process_elements(soup, lambda elem: print(elem.name)) will print only the
names.

You need a bit of error checking to make it work, though.

But wait -- Python's generators let you rewrite process_elements so that you
can use it without a callback:

def gen_elements(elem):
yield elem
for child in elem.children:
yield from gen_elements(child)

for elem in gen_elements(soup):
print(elem.name)

Note that 'yield from iterable' is a shortcut for 'for x in iterable: yield
x', so there are actually two loops in gen_elements().

> Any tips or tricks for this would be grateful. As I could use this in 
> other situations.


___
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] Web scraping using selenium and navigating nested dictionaries / lists.

2019-01-27 Thread mhysnm1964
Marco,

 

Thanks. The reason for learning selenium is for the automation. As I want to 
test web sites for keyboard and mouse interaction and record the results. That 
at least is the long term goal. In the short term, I will have a look at your 
suggestion.

 

 

From: Marco Mistroni  
Sent: Sunday, 27 January 2019 9:46 PM
To: mhysnm1...@gmail.com
Cc: tutor@python.org
Subject: Re: [Tutor] Web scraping using selenium and navigating nested 
dictionaries / lists.

 

Hi my 2 cents. Have a look at scrapy for scraping.selenium is v good  tool to 
learn but is mainly to automate uat of guis

Scrapy will scrape for you and u can automate it via cron. It's same stuff I am 
doing ATM

Hth

On Sun, Jan 27, 2019, 8:34 AM mailto:mhysnm1...@gmail.com>  wrote:

All,



Goal of new project.

I want to scrape all my books from Audible.com that I have purchased.
Eventually I want to export this as a CSV file or maybe Json. I have not got
that far yet. The reasoning behind this is to  learn selenium  for my work
and get the list of books I have purchased. Killing two birds with one stone
here. The work focus is to see if selenium   can automate some of the
testing I have to do and collect useful information from the web page for my
reports. This part of the goal is in the future. As I need to build my
python skills up. 



Thus far, I have been successful in logging into Audible and showing the
library of books. I am able to store the table of books and want to use
BeautifulSoup to extract the relevant information. Information I will want
from the table is:

*   Author 
*   Title
*   Date purchased 
*   Length
*   Is the book in a series (there is a link for this)
*   Link to the page storing the publish details. 
*   Download link

Hopefully this has given you enough information on what I am trying to
achieve at this stage. AS I learn more about what I am doing, I am adding
possible extra's tasks. Such as verifying if I have the book already
download via itunes.



Learning goals:

Using the BeautifulSoup  structure that I have extracted from the page
source for the table. I want to navigate the tree structure. BeautifulSoup
provides children, siblings and parents methods. This is where I get stuck
with programming logic. BeautifulSoup does provide find_all method plus
selectors which I do not want to use for this exercise. As I want to learn
how to walk a tree starting at the root and visiting each node of the tree.
Then I can look at the attributes for the tag as I go. I believe I have to
set up a recursive loop or function call. Not sure on how to do this. Pseudo
code:



Build table structure

Start at the root node.

Check to see if there is any children.

Pass first child to function.

Print attributes for tag at this level 

In function, check for any sibling nodes.

If exist, call function again 

If no siblings, then start at first sibling and get its child.



This is where I get struck. Each sibling can have children and they can have
siblings. So how do I ensure I visit each node in the tree? 

Any tips or tricks for this would be grateful. As I could use this in other
situations.



Sean 

___
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] Web scraping using selenium and navigating nested dictionaries / lists.

2019-01-27 Thread mhysnm1964
All,

 

Goal of new project.

I want to scrape all my books from Audible.com that I have purchased.
Eventually I want to export this as a CSV file or maybe Json. I have not got
that far yet. The reasoning behind this is to  learn selenium  for my work
and get the list of books I have purchased. Killing two birds with one stone
here. The work focus is to see if selenium   can automate some of the
testing I have to do and collect useful information from the web page for my
reports. This part of the goal is in the future. As I need to build my
python skills up. 

 

Thus far, I have been successful in logging into Audible and showing the
library of books. I am able to store the table of books and want to use
BeautifulSoup to extract the relevant information. Information I will want
from the table is:

*   Author 
*   Title
*   Date purchased 
*   Length
*   Is the book in a series (there is a link for this)
*   Link to the page storing the publish details. 
*   Download link

Hopefully this has given you enough information on what I am trying to
achieve at this stage. AS I learn more about what I am doing, I am adding
possible extra's tasks. Such as verifying if I have the book already
download via itunes.

 

Learning goals:

Using the BeautifulSoup  structure that I have extracted from the page
source for the table. I want to navigate the tree structure. BeautifulSoup
provides children, siblings and parents methods. This is where I get stuck
with programming logic. BeautifulSoup does provide find_all method plus
selectors which I do not want to use for this exercise. As I want to learn
how to walk a tree starting at the root and visiting each node of the tree.
Then I can look at the attributes for the tag as I go. I believe I have to
set up a recursive loop or function call. Not sure on how to do this. Pseudo
code:

 

Build table structure

Start at the root node.

Check to see if there is any children.

Pass first child to function.

Print attributes for tag at this level 

In function, check for any sibling nodes.

If exist, call function again 

If no siblings, then start at first sibling and get its child.

 

This is where I get struck. Each sibling can have children and they can have
siblings. So how do I ensure I visit each node in the tree? 

Any tips or tricks for this would be grateful. As I could use this in other
situations.

 

Sean 

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


[Tutor] exporting lists into CSV issue.

2019-01-22 Thread mhysnm1964
All,

 

Thanks in advance for any assistance. This is a follow up msg from my iTunes
import issue with the xml. I have managed to successfully import the data
from the XML with Peter's and Alan's help. I have moved the information into
a list and only extracting the data I want. Now I am trying to export to a
CSV file. There is no syntax or logical errors I can see. The information is
being exported to the CSV. But when I bring it into Excel. I am getting a
blank row between each row. The exported data looks like this:

 

Name,Artist,Kind,Total Time,Year,Date Modified,Date Added,Bit Rate,Sample
Rate,Comments,Location "In Her Sights: A Montgomery Justice Novel, Book 1
(Unabridged)",Robin Perini,Audible file,25574318,2012,2015-12-12
23:48:18,2015-12-12 23:48:20,64,22050,"Jasmine 'Jazz' Parker, Jefferson
County SWAT's only female sniper, can thread the eye of a needle with a
bullet. But she carries with her a secret from her
past","file:///Volumes/Itunes/iTunes/iTunes%20Media/Audiobooks/Robin%20P
erini/In%20Her%20Sights_%20A%20Montgomery%20Justice%20Novel,%20Book%201%20(U
nabridged).aax" "Behind the Lies: A Montgomery Justice Novel, Book 2
(Unabridged)",Robin Perini,Audible file,35142797,2013,2015-12-12
23:53:33,2015-12-12 23:53:33,64,22050,"Of the six Montgomery brothers, Zach
has always walked on the wild side. He rocketed to fame playing a hero in a
movie, but off screen he's living in the shadows. Zach's dark secret: He
leads a double life as a CIA
operative",file:///Volumes/Itunes/iTunes/iTunes%20Media/Audiobooks/Robin
%20Perini/Behind%20the%20Lies_%20A%20Montgomery%20Justice%20Novel,%20Book%20
2%20(Unabridged).aax

 

I would have expected a new line between each book, but there is a space.
The code looks like this:

 

import plistlib, pprint, csv 

 

with open("library.xml", "rb") as f:

data = plistlib.load(f)

 

books =[['Name', 'Artist', 'Kind', 'Total Time', 'Year', 'Date Modified',
'Date Added', 'Bit Rate', 'Sample Rate', 'Comments', 'Location']]

 

for book in list(data['Tracks'].values()):

tmp = []

if not 'Audible file' in book['Kind']:

break # skip book

for key in books[0]:

if key in book:

tmp.append(book[key])

else:

tmp.append("")

books.append(tmp)

 

with open ('books-list.csv', 'w') as wf:

writer = csv.writer(wf)

writer.writerows(books)

wf.close()

 

When I debug the above. The output of books look fine to me. For example:

 

(Pdb)
['Name', 'Artist', 'Kind', 'Total Time', 'Year', 'Date Modified', 'Date
Added', 'Bit Rate', 'Sample Rate', 'Comments', '
Location']
(Pdb)
(Pdb) books[0] 
]
0
['In Her Sights: A Montgomery Justice Novel, Book 1 (Unabridged)', 'Robin
Perini', 'Audible file', 25574318, 2012, datet
ime.datetime(2015, 12, 12, 23, 48, 18), datetime.datetime(2015, 12, 12, 23,
48, 20), 64, 22050, "Jasmine 'Jazz' Parker,
Jefferson County SWAT's only female sniper, can thread the eye of a needle
with a bullet. But she carries with her a sec
ret from her past",
'file:///Volumes/Itunes/iTunes/iTunes%20Media/Audiobooks/Robin%20Perini/In%2
0Her%20Sights_%20A%2
0Montgomery%20Justice%20Novel,%20Book%201%20(Unabridged).aax']
(Pdb)
(Pdb) books[1] 
]
1
['Behind the Lies: A Montgomery Justice Novel, Book 2 (Unabridged)', 'Robin
Perini', 'Audible file', 35142797, 2013, dat
etime.datetime(2015, 12, 12, 23, 53, 33), datetime.datetime(2015, 12, 12,
23, 53, 33), 64, 22050, 'Of the six Montgomery
brothers, Zach has always walked on the wild side. He rocketed to fame
playing a hero in a movie, but off screen he's l
iving in the shadows. Zach's dark secret: He leads a double life as a CIA
operative', 'file:///Volumes/Itunes/iTunes
/iTunes%20Media/Audiobooks/Robin%20Perini/Behind%20the%20Lies_%20A%20Montgom
ery%20Justice%20Novel,%20Book%202%20(Unabrid
ged).aax']
(Pdb)

 

Any ideas why this could be occurring? 

 

Regards

Sean 

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


Re: [Tutor] Importing XML files.

2019-01-21 Thread mhysnm1964
Peter and Alan,

Peter, Thanks for the information. The library did the trick and I can get 
access to the XML content.

Alan, thanks for the explanation of the tree structure. I was aware of this 
already for HTML and XML. Just didn't understand the terminology used from the 
XML library. The tutorials I have viewed didn't explain this information. I 
will continue investigating. The library provided has addressed my concerns for 
now.


-Original Message-
From: Tutor  On Behalf Of Peter 
Otten
Sent: Tuesday, 22 January 2019 5:22 AM
To: tutor@python.org
Subject: Re: [Tutor] Importing XML files.

mhysnm1...@gmail.com wrote:

> I am trying to import ITunes XML files. I have been doing some reading 
> and I am somewhat confused with some XML terminology.

> What is:
> 
>  
> 
> Tag – I think it is the  in the itunes library file.
> 
> Text = The text between the 
> 
> Attrib  -- don’t know.
> 
>  
> 
> If I am correct in my thinking. If you look up all the tags using the 
> xml module. How do you then isolate all the name text? I do not have 
> any working code at this present time. I have been playing with the 
> xml methods and reading some example tutorials. But I am stumped with 
> the above terminology which is a small road block. Below is an example 
> extract of my xML ITunes to see if this helps. I am doing this in 
> Python 3.7 for Windows 10.
> 
>  
> 
> 
> 
>  "http://www.apple.com/DTDs/PropertyList-1.0.dtd;>
> 
> 

You may be lucky in that you can avoid the hard way outlined by Alan -- 
Python's stdandard library includes a module that handles Apple's plist format. 
I tried your sample, and the module seems to turn it into nested
dicts:

>>> import plistlib, pprint
>>> with open("sample.xml", "rb") as f:
... data = plistlib.load(f)
... 
>>> pprint.pprint(data, width=70)
{'Application Version': '12.8.0.150',
 'Date': datetime.datetime(2019, 1, 14, 3, 56, 30),
 'Features': 5,
 'Library Persistent ID': 'F2D33B339F0788F0',  'Major Version': 1,  'Minor 
Version': 1,  'Music Folder': 'file:///Volumes/Itunes/iTunes/iTunes%20Media/',
 'Show Content Ratings': True,
 'Tracks': {'6493': {'Album': 'In Her Sights (Unabridged)',
 'Album Artist': 'Robin Perini',
 'Artist': 'Robin Perini',
 'Artwork Count': 1,
 'Bit Rate': 64,
 'Comments': "Jasmine 'Jazz' Parker, "
 'Jefferson County SWAT’s only '
 'female sniper, can thread the '
 'eye of a needle with a bullet. '
 'But she carries with her a '
 'secret from her past', [snip]
>>> data["Date"].isoformat()
'2019-01-14T03:56:30'
>>> list(data["Tracks"].values())[0]["Artist"]
'Robin Perini'

Looks good, except for the mojibake -- but that was already in your email.


___
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] Importing XML files.

2019-01-21 Thread mhysnm1964
All,

 

I am trying to import ITunes XML files. I have been doing some reading and I am 
somewhat confused with some XML terminology.

 

What is:

 

Tag – I think it is the  in the itunes library file.

Text = The text between the 

Attrib  -- don’t know.

 

If I am correct in my thinking. If you look up all the tags using the xml 
module. How do you then isolate all the name text? I do not have any working 
code at this present time. I have been playing with the xml methods and reading 
some example tutorials. But I am stumped with the above terminology which is a 
small road block. Below is an example extract of my xML ITunes to see if this 
helps. I am doing this in Python 3.7 for Windows 10. 

 



http://www.apple.com/DTDs/PropertyList-1.0.dtd;>





  Major Version1

  Minor Version1

  Date2019-01-14T03:56:30Z

  Application Version12.8.0.150

  Features5

  Show Content Ratings

  Music 
Folderfile:///Volumes/Itunes/iTunes/iTunes%20Media/

  Library Persistent IDF2D33B339F0788F0

  Tracks

  

   6493

   

 Track 
ID6493

 NameIn Her Sights: 
A Montgomery Justice Novel, Book 1 (Unabridged)

 ArtistRobin 
Perini

 Album ArtistRobin 
Perini

 AlbumIn Her Sights 
(Unabridged)

 
GenreAudiobook

 KindAudible 
file

 
Size206806038

 Total 
Time25574318

 Year2012

 Date 
Modified2015-12-12T23:48:18Z

 Date 
Added2015-12-12T23:48:20Z

 Bit 
Rate64

 Sample 
Rate22050

 CommentsJasmine 
'Jazz' Parker, Jefferson County SWAT’s only female sniper, can thread the eye 
of a needle with a bullet. But she carries with her a secret from her 
past

 
Normalization790

 Artwork 
Count1

 Persistent 
ID2C4CC3C31A2B95B5

 Track 
TypeFile

 Protected

   
Locationfile:///Volumes/Itunes/iTunes/iTunes%20Media/Audiobooks/Robin%20Perini/In%20Her%20Sights_%20A%20Montgomery%20Justice%20Novel,%20Book%201%20(Unabridged).aax

 File Folder 
Count4

 Library Folder 
Count1

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


Re: [Tutor] Debugging a sort error.

2019-01-13 Thread mhysnm1964
Peter,

Thanks for the code for a custom key. That will come in handy later down the
track.

-Original Message-
From: Tutor  On Behalf Of
Peter Otten
Sent: Sunday, 13 January 2019 10:00 PM
To: tutor@python.org
Subject: Re: [Tutor] Debugging a sort error.

mhysnm1...@gmail.com wrote:

> Issue, following error is generated after trying to sort a list of 
> strings.
> 
> description.sort()
> TypeError: unorderable types: float() < str()

Consider

>>> descriptions = ["foo", "bar", 123, 3.14, 42, 200.1, "0"]
>>> sorted(descriptions)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unorderable types: int() < str()

If there are only numbers and strings in the list you can force the sort to
succeed with the following custom key function:

>>> def key(item):
... return isinstance(item, str), item
... 

This will move the numbers to the beginning of the list:

>>> sorted(descriptions, key=key)
[3.14, 42, 123, 200.1, '0', 'bar', 'foo']


___
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] Debugging a sort error.

2019-01-13 Thread mhysnm1964
All,
Once again thanks for all the suggestions. It was the input data after all. As 
I am importing three sheets into python. One of the sheets had one less column. 
Thus how I ended up with float points. The testing for the string helped here 
greatly.  Now things are correct again. 

On and forward to start working on text pattern exercise which I always have 
struggled with. Last language I did this in was Perl and had all sorts of 
headaches.  Python seems cleaner from the reading I have done thus far. Lets 
see what challenges wait in front of me.




-Original Message-
From: Stephen Nelson-Smith  
Sent: Monday, 14 January 2019 1:15 AM
To: mhysnm1...@gmail.com
Cc: Python Tutor mailing list 
Subject: Re: [Tutor] Debugging a sort error.

Hi,

On Sun, Jan 13, 2019 at 8:34 AM  wrote:

> description.sort()
> TypeError: unorderable types: float() < str()

So, fairly obviously, we can't test whether a float is less than a string.  Any 
more than we can tell if a grapefruit is faster than a cheetah.  So there must 
be items in description that are strings and floats.

With 2000 lines, you're going to struggle to eyeball this, so try something 
like this:

In [69]: irrational_numbers = [3.14159265, 1.606695, "pi", "Pythagoras 
Constant"] In [70]: from collections import Counter In [71]: 
dict(Counter([type(e) for e in irrational_numbers]))
Out[71]: {float: 2, str: 2}

If with your data, this shows only strings, I'll eat my hat.

S.

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


Re: [Tutor] Debugging a sort error.

2019-01-13 Thread mhysnm1964
Everyone,

I did find out the issue. When looking at the output in a spreadsheet. I was 
inserting floats into the description dictionary from the code I was using to 
extract the data. Thus I have gone back to the drawing board. Rather than 
extracting by columns which became difficult to achieve what I want to do. I am 
now extracting by row and the code has greatly simplified.

Sorry about the data example. AS I am dealing with personal information. I 
don't want to share the original data. I will look at my editor to see if it is 
able to change the indent.

Note: I am a Vision Impaired (blind) person learning to code in Python. Thus 
indents don't really matter to me.  But I will change the indents to make it 
easier to read.

Thanks for the test, this will help greatly.


-Original Message-
From: Cameron Simpson  
Sent: Sunday, 13 January 2019 8:12 PM
To: mhysnm1...@gmail.com
Cc: Tutor@python.org
Subject: Re: [Tutor] Debugging a sort error.

Discussion inline below.

On 13Jan2019 13:16, mhysnm1...@gmail.com  wrote:
>I am hoping someone can help with the below error using Python3.5 in 
>the Windows 10 bash environment. I found the below link which I am not 
>sure if this is related to the issue or not. As I don't fully understand the 
>answer.
>
>https://github.com/SethMMorton/natsort/issues/7

I'm not sure that URL is very relevant, except to the extent that it points out 
that Python 3 issues an error when comparing incomparable types. In Python 2 
this problem could go unnoticed, and that just leads to problems later, much 
farther from the source of the issue.

>Issue, following error is generated after trying to sort a list of strings.
>
>description.sort()
>TypeError: unorderable types: float() < str()
>
>Each list items (elements) contain a mixture of alpha chars, numbers, 
>punctuation chars like / and are in a string type. Below is an example 
>extract of the data from the list.
>
>['Description', 'EFTPOS WOOLWORTHS  1294 ", "withdrawal Hudson
>street 3219"]

The error message says that some of these values are not strings. One at least 
is a float.

My expectation is that the openpyxl module is reading a floating point value 
into your description array. This might be openpxyl being too clever, or it 
might be (more likely IMO) be Excel turning something that looked like a float 
into a float. Spreadsheets can be ... helpful like that.

>There is over 2000 such entries. This used to work and now doesn't.  

You'll need to examine the values. But I see that you're trying to do this. 
I've snipped the data loading phase. Here:

>description = data['Description']
>for i in description:
>  if not str(i):
>print "not a string")

This is not a valid check that "i" is a string. That expression:

  str(i)

tries to convert "i" into a string (via its __str__ method). Most objects have 
such a method, and str() of a float is the textual representation of the float. 
So the if statement doesn't test what you want to test. Try this:

  if not isinstance(i, str):
print("not a string:", i, type(i))

>description.sort()
>I am suspecting it is something to do with the data but cannot track 
>down the cause. Any suggestions on how to debug this?

Your exception is in here, but as you expect you want to inspect the 
description types first.

If the description column does contain a float in the original data then you 
could convert it to a string first! Note that this may not match visually what 
was in the spreadsheet. (BTW, your cited code never fills out the description 
list, not it cannot be current.)

But first, fine out what's wrong. Try the type test I suggest and see how far 
you get.

Cheers,
Cameron Simpson 

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


[Tutor] Debugging a sort error.

2019-01-13 Thread mhysnm1964
Hello everyone.

 

I am hoping someone can help with the below error using Python3.5 in the
Windows 10 bash environment. I found the below link which I am not sure if
this is related to the issue or not. As I don't fully understand the answer.

https://github.com/SethMMorton/natsort/issues/7

 

Issue, following error is generated after trying to sort a list of strings.

 

description.sort()
TypeError: unorderable types: float() < str()

 

Each list items (elements) contain a mixture of alpha chars, numbers,
punctuation chars like / and are in a string type. Below is an example
extract of the data from the list.

 

['Description', 'EFTPOS WOOLWORTHS  1294 ", "withdrawal Hudson
street 3219"] 

 

There is over 2000 such entries. This used to work and now doesn't. Only
change to the code was modifying the data variable from a list to a
dictionary. Below is the full code:

 

import openpyxl 

from openpyxl.utils import get_column_letter

from  more_itertools import unique_everseen

 

# Loding the workbook and names for the sheets.

wb = openpyxl.load_workbook("test.xlsx")

wss = wb.get_sheet_names()

 

description = [] # all the descriptions

data = {}

 

for ws_name in wss:

  ws = wb.get_sheet_by_name(ws_name)

  for column in ws.columns:

   col_data = [] # column list

   for cell in column:

 value = cell.value

 col_data.append(value)

   # end for cell loop

   if ws_name == "WestPac":

 if col_data[0] == 'Credit Amount':

   num = 1

   while num <=
(len(col_data) - 1):

 value =
col_data[num]

 if
value:

 
data['Amount'][num] = value

 else:

 
data['Amount'][num] = data['Amount'][num] * -1

 num +=
1

   # end while num 

   break

 # end if 

   # end if

   if col_data[0] in data:

 
data[col_data[0]].extend(col_data[1:-1])

   else:

 data[col_data[0]] = col_data

  # end for column

#end for ws_name 

 

description = data['Description']

for i in description:

  if not str(i):

print "not a string")

description.sort()

 

I am suspecting it is something to do with the data but cannot track down
the cause. Any suggestions on how to debug this?

 

Sean 

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


[Tutor] Data pattern query.

2019-01-07 Thread mhysnm1964
All,

 

I am currently writing a Python program to identify common text patterns in a 
excel spreadsheet which I have imported using openpyxl. All the descriptions of 
the transactions are in a single column. I am trying to work out the easiest 
method of identifying the same pattern of text in the fields. End of the day, I 
want a list of all the vendor transaction names. I don’t care if they are a 
credit or debit at this stage. Then I am going to group these vendors by 
categories. All this data has been downloaded from my bank originally.

 

In the field, there is the vendor name, suburb/town, type of transaction, etc.

 

I am only interested in the vendor name. So the vendor could be called “a”, 
“b”, “c”, etc. As I don’t know all the different vendors. How can I teach the 
program to learn new vendor names? I was thinking of removing all the duplicate 
entries as a start. Was thinking of using dictionaries for this. But not sure 
if this is the best approach. 

 

I am really stuck and if there is a library out there which can do this for me. 
I would be grateful. 

 

Note, I am new to Python and not an advance programmer.

 

Sean 

 

 

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