Re: Get Count of function arguments passed in

2019-09-11 Thread Sayth Renshaw
On Wednesday, 11 September 2019 20:25:32 UTC+10, Sayth Renshaw  wrote:
> On Wednesday, 11 September 2019 20:11:21 UTC+10, Sayth Renshaw  wrote:
> > Hi
> > 
> > I want to allow as many lists as needed to be passed into a function.
> > But how can I determine how many lists have been passed in?
> > 
> > I expected this to return 3 but it only returned 1.
> > 
> > matrix1 = [[1, -2], [-3, 4],]
> > matrix2 = [[2, -1], [0, -1]]
> > matrix3 = [[2, -1], [0, -1]]
> > # print(add(matrix1, matrix2))
> > 
> > def add(*matrix):
> >     print(len(locals()))
> > 
> > add(matrix1,matrix2,matrix3)
> > 
> > Cheers
> > 
> > Sayth
> 
> Tried creating a list of the arguments, however I am creating too many 
> positional arguments.
> 
> matrix1 = [[1, -2], [-3, 4],]
> matrix2 = [[2, -1], [0, -1]]
> matrix3 = [[2, -1], [0, -1]]
> 
> matrix = []
> def add_many(a = list(*matrix)):
> for num in a:
> for i in range(len(matrix[num])):
> for j in range(len(matrix[num])):
> print(matrix[num][i][j] + matrix[num][i][j])
> 
> add_many(matrix1,matrix2)

Last failure for the moment

matrix1 = [[1, -2], [-3, 4],]
matrix2 = [[2, -1], [0, -1]]
matrix3 = [[2, -1], [0, -1]]

matrix = []
def add_many(*matrix):
for num in range(add_many().func_code.co_argcount):
    for i in range(len(matrix[num])):
for j in range(len(matrix[num])):
print(matrix[i][j] + matrix[i][j])

add_many(matrix1,matrix2)

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


Re: Get Count of function arguments passed in

2019-09-11 Thread Sayth Renshaw
On Wednesday, 11 September 2019 20:11:21 UTC+10, Sayth Renshaw  wrote:
> Hi
> 
> I want to allow as many lists as needed to be passed into a function.
> But how can I determine how many lists have been passed in?
> 
> I expected this to return 3 but it only returned 1.
> 
> matrix1 = [[1, -2], [-3, 4],]
> matrix2 = [[2, -1], [0, -1]]
> matrix3 = [[2, -1], [0, -1]]
> # print(add(matrix1, matrix2))
> 
> def add(*matrix):
> print(len(locals()))
> 
> add(matrix1,matrix2,matrix3)
> 
> Cheers
> 
> Sayth

Tried creating a list of the arguments, however I am creating too many 
positional arguments.

matrix1 = [[1, -2], [-3, 4],]
matrix2 = [[2, -1], [0, -1]]
matrix3 = [[2, -1], [0, -1]]

matrix = []
def add_many(a = list(*matrix)):
for num in a:
for i in range(len(matrix[num])):
for j in range(len(matrix[num])):
print(matrix[num][i][j] + matrix[num][i][j])

add_many(matrix1,matrix2)
-- 
https://mail.python.org/mailman/listinfo/python-list


Get Count of function arguments passed in

2019-09-11 Thread Sayth Renshaw
Hi

I want to allow as many lists as needed to be passed into a function.
But how can I determine how many lists have been passed in?

I expected this to return 3 but it only returned 1.

matrix1 = [[1, -2], [-3, 4],]
matrix2 = [[2, -1], [0, -1]]
matrix3 = [[2, -1], [0, -1]]
# print(add(matrix1, matrix2))

def add(*matrix):
print(len(locals()))

add(matrix1,matrix2,matrix3)

Cheers

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


Re: pandas loc on str lower for column comparison

2019-09-09 Thread Sayth Renshaw
On Tuesday, 10 September 2019 12:56:36 UTC+10, Sayth Renshaw  wrote:
> On Friday, 6 September 2019 07:52:56 UTC+10, Piet van Oostrum  wrote:
> > Piet van Oostrum <> writes:
> > 
> > > That would select ROWS 0,1,5,6,7, not columns.
> > > To select columns 0,1,5,6,7, use two-dimensional indexes
> > >
> > > df1 = df.iloc[:, [0,1,5,6,7]]
> > >
> > > : selects all rows.
> > 
> > And that also solves your original problem.
> > 
> > This statement:
> > 
> > df1['Difference'] = df1.loc['Current Team'].str.lower().str.strip() == 
> > df1.loc['New Team'].str.lower().str.strip()
> > 
> > should not use .loc, because then you are selecting rows, not columns, but:
> > 
> > df1['Difference'] = df1['Current Team'].str.lower().str.strip() == df1['New 
> > Team'].str.lower().str.strip()
> > -- 
> > Piet van Oostrum <>
> > WWW: http://piet.vanoostrum.org/
> > PGP key: [8DAE142BE17999C4]
> 
> That actually creates another error.
> 
> A value is trying to be set on a copy of a slice from a DataFrame.
> Try using .loc[row_indexer,col_indexer] = value instead
> 
> See the caveats in the documentation: 
> http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
> 
> So tried this
> df['c'] = df.apply(lambda df1: df1['Current Team'].str.lower().str.strip() == 
> df1['New Team'].str.lower().str.strip(), axis=1)
> 
> Based on this SO answer https://stackoverflow.com/a/46570641
> 
> Thoughts?
> 
> Sayth

This works on an individual row
df2 = df1.loc[(df1['Current Team'] == df1['New Team']),'D'] = 'Wow'

But how do I apply it to the whole new column and return the new dataset?

Trying to use lambda but it cannot contain assigment
df2 = df1.apply(lambda df1: [ (df1['Current Team'] == df1['New Team'])  ]['D'] 
= 'succeed')
df2

Confused

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


Re: pandas loc on str lower for column comparison

2019-09-09 Thread Sayth Renshaw
On Friday, 6 September 2019 07:52:56 UTC+10, Piet van Oostrum  wrote:
> Piet van Oostrum <> writes:
> 
> > That would select ROWS 0,1,5,6,7, not columns.
> > To select columns 0,1,5,6,7, use two-dimensional indexes
> >
> > df1 = df.iloc[:, [0,1,5,6,7]]
> >
> > : selects all rows.
> 
> And that also solves your original problem.
> 
> This statement:
> 
> df1['Difference'] = df1.loc['Current Team'].str.lower().str.strip() == 
> df1.loc['New Team'].str.lower().str.strip()
> 
> should not use .loc, because then you are selecting rows, not columns, but:
> 
> df1['Difference'] = df1['Current Team'].str.lower().str.strip() == df1['New 
> Team'].str.lower().str.strip()
> -- 
> Piet van Oostrum <>
> WWW: http://piet.vanoostrum.org/
> PGP key: [8DAE142BE17999C4]

That actually creates another error.

A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: 
http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

So tried this
df['c'] = df.apply(lambda df1: df1['Current Team'].str.lower().str.strip() == 
df1['New Team'].str.lower().str.strip(), axis=1)

Based on this SO answer https://stackoverflow.com/a/46570641

Thoughts?

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


Re: pandas loc on str lower for column comparison

2019-09-05 Thread Sayth Renshaw
That is actually consistent with Excel row, column. Can see why it works that 
way then.

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


Re: pandas loc on str lower for column comparison

2019-09-04 Thread Sayth Renshaw
On Sunday, 1 September 2019 10:48:54 UTC+10, Sayth Renshaw  wrote:
> I've created a share doc same structure anon data from my google drive.
> 
> https://drive.google.com/file/d/0B28JfFTPNr_lckxQRnFTRF9UTEFYRUVqRWxCNVd1VEZhcVNr/view?usp=sharing
> 
> Sayth

I tried creating the df1 dataframe by using iloc instead of loc to avoid any 
column naming issues.

So i created a list of integers for iloc representing the columns in current 
example.

df1 = df.iloc[[0,1,5,6,7]] 

However, I ust be misunderstanding the docs 
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.iloc.html#pandas.DataFrame.iloc
Allowed inputs are:

An integer, e.g. 5.
A list or array of integers, e.g. [4, 3, 0].

Because while it works I appear to grab all columns 13 when I requested 5.
UID NameFTE Agent IDCurrent Leader  New Leader  
Current TeamNew TeamCurrent SiteNew SiteUnnamed: 10 
Unnamed: 11 Unnamed: 12 

How do I misunderstand iloc?

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


Re: Need help: integrating unittest with setuptools

2019-09-01 Thread Sayth Renshaw
On Monday, 2 September 2019 04:44:29 UTC+10, YuXuan Dong  wrote:
> Hi, everybody:
> 
> I have met a problem while I ran `python setup.py test`:
> 
>   unittest.case.SkipTest: No module named 'winreg'
> 
> I ran the command in MacOS and my project is written for only UNIX-like 
> systems. I don't use any Windows-specified API. How dose `winreg` come here?
> 
> In my `setup.py`:
> 
>   test_suite="test"
> 
> In my `test/test.py`:
> 
>   import unittest
> 
>   class TestAll(unittest.TestCase):
>   def testall(self):
>   return None
> 
> It works if I ran `python -m uniittest test.py` alone but raises the above 
> exception if I ran `python setup.py test`.
> 
> I'm working on this for the whole day, searching for every keywords I can 
> think of with Google but can't find why or how. Could you help me? Thanks.
> 
> --
> YX. D.

Does this help?
https://stackoverflow.com/questions/4320761/importerror-no-module-named-winreg-python3

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


Re: How to create a list and append to list inside a mqtt and GUI program?

2019-09-01 Thread Sayth Renshaw
me__ == "__main__": 
> app = QApplication(sys.argv) 
> mainWin = MainWindow() 
> mainWin.show() 
> publishedMessage = mainWin.getGUIFilename() 
> sys.exit(app.exec_()) 
> 
> MQTT.py 
> import logging 
> from datetime import timedelta 
> import time 
> from thespian.actors import * 
> from transitions import Machine 
> import paho.mqtt.client as mqtt 
> import importlib 
> import os.path 
> import sys 
> from PyQt5.QtWidgets import * 
> from PyQt5.QtCore import * 
> from PyQt5 import QtWidgets, uic 
> 
> class device(mqtt.Client): 
> def on_connect(self, mqttc, obj, flags, rc): 
> if rc == 0: 
> print("Connected to broker") 
> else: 
> print("Connection failed") 
> 
> # mqttc.subscribe("microscope/light_sheet_microscope/UI") 
> 
> def on_message(self, mqttc, userdata, message): 
> msg = str(message.payload.decode("utf-8")) 
> print("message recieved= " + msg) 
> # print("File which you want to import(with .py extension)") 
> print("message topic=", message.topic) 
> print("message qos=", message.qos) 
> print("message retain flag=", message.retain) 
> 
> def run(self): 
> self.connect("broker.hivemq.com", 1883, 60) 
> 
> test1.py
> 
> from PyQt5 import QtCore, QtGui, QtWidgets
> import sys
> from PyQt5.QtWidgets import *
> from PyQt5.QtCore import *
> from PyQt5 import QtWidgets, uic
> from mqtt import *
> 
> class SubWindow(QWidget):
> def __init__(self, parent = None):
> super(SubWindow, self).__init__(parent)
> self.setMinimumSize(QSize(300, 200))
> label = QLabel("Laser",  self)
> 
> self.modeButton = QtWidgets.QPushButton("Click me",self)
> self.modeButton.setGeometry(QtCore.QRect(10, 40, 81, 23))
> self.modeButton.setObjectName("Turn on")
> self.modeButton.clicked.connect(self.modFun)
> 
> def modFun(self):
> print("creating new instance " + "Laser")
> client = device("Laser")
> client.run()
> 
> client.loop_start()  # start the loop
> device_message = "ON"
> time.sleep(2)
> print("Subscribing to topic", 
> "microscope/light_sheet_microscope/UI")
> client.subscribe("microscope/light_sheet_microscope/UI")
> print("Publishing message to topic", 
> "microscope/light_sheet_microscope/UI")
> client.publish("microscope/light_sheet_microscope/UI", 
> device_message)
> time.sleep(2)  # wait
> client.loop_stop()  # stop the loop
> 
> def closeEvent(self, event):
> self.close()
> 
> test2.py
> 
> from PyQt5 import QtCore, QtGui, QtWidgets
> import sys
> from PyQt5.QtWidgets import *
> from PyQt5.QtCore import *
> from PyQt5 import QtWidgets, uic
> 
> class SubWindow(QWidget):
> def __init__(self, parent = None):
> super(SubWindow, self).__init__(parent)
> self.setMinimumSize(QSize(300, 250))
> label = QLabel("Sub Window",  self)
> 
> self.modeButton = QtWidgets.QPushButton("Click me",self)
> self.modeButton.setGeometry(QtCore.QRect(10, 40, 81, 23))
> 
> self.modeButton.clicked.connect(self.modFun)
> 
> 
> 
> def modFun(self):
> print("Hello there i'm Click me")
> 
> def closeEvent(self, event):
> self.close()
> 
> Thanks.

list_of_file_name = []
my_file = getGUIFilename()
list.append(my_file)

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


Re: pandas loc on str lower for column comparison

2019-09-01 Thread Sayth Renshaw



I've created a share doc same structure anon data from my google drive.

https://drive.google.com/file/d/0B28JfFTPNr_lckxQRnFTRF9UTEFYRUVqRWxCNVd1VEZhcVNr/view?usp=sharing

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


Re: pandas loc on str lower for column comparison

2019-08-31 Thread Sayth Renshaw
On Sunday, 1 September 2019 05:19:34 UTC+10, Piet van Oostrum  wrote:
> Sayth Renshaw writes:
> 
> > But on both occasions I receive this error.
> >
> > # KeyError: 'the label [Current Team] is not in the [index]'
> >
> > if I test df1 before trying to create the new column it works just fine.
> >
> What do you mean by testing df1?
> 
> And could it be that 'Current Team' is spelled differently in the assignment 
> than in the construction of df1? For example a difference in spaces, like a 
> triling space or a breaking vs. non-breaking space? Please check that both 
> are exactly the same.
> 
> -- 
> Piet van Oostrum 
> WWW: http://piet.vanoostrum.org/
> PGP key: [8DAE142BE17999C4]

Hi

Version info

1.1.0 #scipy
0.23.4 #pandas

RangeIndex: 35 entries, 0 to 34
Data columns (total 5 columns):
UID 35 non-null object
Name35 non-null object
New Leader  35 non-null object
Current Team35 non-null object
New Team35 non-null object
dtypes: object(5)
memory usage: 1.4+ KB

I had to anonymise the sheet. But same structure. tested produces same errors.

import pandas as pd
import scipy

print(scipy.version.version)
print(pd.__version__)
xls = pd.ExcelFile("Melbourne-anon.xlsx")
df = xls.parse('Sheet1', skiprows= 4)
# df1 = df.loc[:, ('UID','Name','New Leader','Current Team', 'New Team')]
# print(df['Current Team'] == df1['Current Team'])
df1 = df.loc[:, ('UID','Name','New Leader','Current Team', 'New Team')] 
# df1['Difference'] = df1.loc['Current Team'].str.lower().str.strip() == 
df1.loc['New Team'].str.lower().str.strip()
# df1 = df[['UID','Name','New Leader','Current Team', 'New Team']].copy()
df1['Difference'] = df1.loc['Current Team'].str.lower().str.strip() == 
df1.loc['New Team'].str.lower().str.strip()
df1.info()


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


Re: pandas loc on str lower for column comparison

2019-08-29 Thread Sayth Renshaw
On Friday, 30 August 2019 00:49:32 UTC+10, Piet van Oostrum  wrote:
> Piet van Oostrum  writes:
> 
> > So the correct way to do this is to make df1 a copy rather than a view.
> >
> > df1 = df.loc[:, ('UID','Name','New Leader','Current Team', 'New Team')]
> 
> Or maybe even make an explicit copy:
> 
> df1 = df[['UID','Name','New Leader','Current Team', 'New Team']].copy()
> -- 
> Piet van Oostrum
> WWW: http://piet.vanoostrum.org/
> PGP key: [8DAE142BE17999C4]

I have tried both

df1 = df.loc[:, ('UID','Name','New Leader','Current Team', 'New Team')] 
df1['Difference'] = df1.loc['Current Team'].str.lower().str.strip() == 
df1.loc['New Team'].str.lower().str.strip()

and 

df1 = df[['UID','Name','New Leader','Current Team', 'New Team']].copy()
df1['Difference'] = df1.loc['Current Team'].str.lower().str.strip() == 
df1.loc['New Team'].str.lower().str.strip()

But on both occasions I receive this error.

# KeyError: 'the label [Current Team] is not in the [index]'

if I test df1 before trying to create the new column it works just fine.

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


Re: [SOLVED] Re: Compare zip lists where order is important

2019-08-29 Thread Sayth Renshaw
On Thursday, 29 August 2019 20:33:46 UTC+10, Peter Otten  wrote:
> Sayth Renshaw wrote:
> 
> > will find the added
> > pairs, but ignore the removed ones. Is that what you want?
> > 
> > Yes, I think. I want to find the changed pairs. The people that moved team
> > numbers.
> 
> To find the people that moved team numbers I would tear the pairs apart. 
> Like:
> 
> >>> people = ["Tim","Bill","Sally","Ally","Fred","Fredricka"]
> >>> team_number = [1,1,2,2,3,3]
> >>> shuffle_people = ["Fredricka","Bill","Sally","Tim","Ally","Fred"]
> >>> shuffle_team_number = [1,1,2,2,3,3]
> >>> old = dict(zip(people, team_number))
> >>> new = dict(zip(shuffle_people, shuffle_team_number))
> >>> for name in old.keys() & new.keys():
> ... old_team = old[name]
> ... new_team = new[name]
> ... if old_team != new_team:
> ... print(name, "went from", old_team, "to", new_team)
> ... 
> Tim went from 1 to 2
> Fredricka went from 3 to 1
> Ally went from 2 to 3

The reason I opted away from Dictionaries is if there was a team with people 
with same name. Then the keys would be the same.

So if Sally left and team 2 had one Tim move in and a new Tim start.
shuffle_people = ["Fredricka","Bill","Tim","Tim","Ally","Fred"] 
shuffle_team_number = [1,1,2,2,3,3] 

becomes 
{'Fredricka': 1, 'Bill': 1, 'Tim': 2, 'Ally': 3, 'Fred': 3}

This still appears to work but is wrong.

for name in old.keys()& new.keys():
old_team = old[name]
new_team = new[name]
if old_team != new_team:
print(name, "went from", old_team, "to", new_team)
Ally went from 2 to 3
Tim went from 1 to 2
Fredricka went from 3 to 1

But I guess in reality I would use a UID and then look up the UID in a list or 
database.

Cheers

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


Re: pandas loc on str lower for column comparison

2019-08-29 Thread Sayth Renshaw
On Friday, 30 August 2019 00:49:32 UTC+10, Piet van Oostrum  wrote:
> Piet van Oostrum 
> 
> > So the correct way to do this is to make df1 a copy rather than a view.
> >
> > df1 = df.loc[:, ('UID','Name','New Leader','Current Team', 'New Team')]
> 
> Or maybe even make an explicit copy:
> 
> df1 = df[['UID','Name','New Leader','Current Team', 'New Team']].copy()

Thank you so much.

Sayth

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


Re: [SOLVED] Re: Compare zip lists where order is important

2019-08-29 Thread Sayth Renshaw
will find the added 
pairs, but ignore the removed ones. Is that what you want? 

Yes, I think. I want to find the changed pairs. The people that moved team 
numbers.

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


pandas loc on str lower for column comparison

2019-08-28 Thread Sayth Renshaw


Hi

I am importing 4 columns into a dataframe from a spreadsheet.

My goal is to create a 5th column with TRUE or False if column 4 (str) matches 
column 3.

Trying to leverage this answer https://stackoverflow.com/a/35940955/461887

This is my code 

import pandas as pd

xls = pd.ExcelFile("Melbourne.xlsx")
df = xls.parse('Sheet1', skiprows= 4)
df1 = df[['UID','Name','New Leader','Current Team', 'New Team']]
df1['Difference'] = df1['Current Team'].str.lower().str.replace('s/+',"") == 
df1['New Team'].str.lower().str.replace('s/+',"")

Which gives this error

C:\Users\u369811\AppData\Local\Continuum\anaconda3\lib\site-packages\ipykernel_launcher.py:6:
 SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: 
http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

If I update the line to use loc as this I still receive a long error.

df1['Difference'] = df1.loc['Current Team'].str.lower().str.replace('s/+',"") 
== df1.loc['New Team'].str.lower().str.replace('s/+',"")

What am I missing?

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


[SOLVED] Re: Compare zip lists where order is important

2019-08-28 Thread Sayth Renshaw
On Thursday, 29 August 2019 14:03:44 UTC+10, Sayth Renshaw  wrote:
> On Thursday, 29 August 2019 13:53:43 UTC+10, Sayth Renshaw  wrote:
> > On Thursday, 29 August 2019 13:25:01 UTC+10, Sayth Renshaw  wrote:
> > > Hi
> > > 
> > > Trying to find whats changed in this example. Based around work and team 
> > > reschuffles.
> > > 
> > > So first I created my current teams and then my shuffled teams.
> > > 
> > > people = ["Tim","Bill","Sally","Ally","Fred","Fredricka"]
> > > team_number = [1,1,2,2,3,3]
> > > 
> > > shuffle_people = ["Fredricka","Bill","Sally","Tim","Ally","Fred"]
> > > shuffle_team_number = [1,1,2,2,3,3]
> > > 
> > > Then combine.
> > > 
> > > teams = list(zip(people,team_number))
> > > shuffle_teams = list(zip(shuffle_people, shuffle_team_number))
> > > 
> > > Then I am attempting to compare for change.
> > > 
> > > [i for i, j in zip(teams, shuffle_teams) if i != j]
> > > 
> > > #Result
> > > [('Tim', 1), ('Ally', 2), ('Fred', 3), ('Fredricka', 3)]
> > > 
> > > #Expecting to see
> > > 
> > > [('Fredricka', 1),('Tim', 2)]
> > > 
> > > What's a working way to go about this?
> > > 
> > > Sayth
> > 
> > It looks like Tuples are comparing by position changes not content changes.
> > 
> > So this fails too
> > 
> > set(shuffle_teams) & set(teams)
> > # {('Bill', 1), ('Fred', 3), ('Sally', 2)}
> 
> Well this works although its not clear which line is the change.
> 
> set(teams).symmetric_difference(set(shuffle_teams))
> 
> {('Ally', 2),
>  ('Ally', 3), # This is the change Ally changed from 2 to 3
>  ('Fredricka', 1), # However here this first line is the change.
>  ('Fredricka', 3),
>  ('Tim', 1),
>  ('Tim', 2)}
> 
> Hints?

set(shuffle_teams).difference(set(teams))
{('Ally', 3), ('Fredricka', 1), ('Tim', 2)}

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


Re: Compare zip lists where order is important

2019-08-28 Thread Sayth Renshaw
On Thursday, 29 August 2019 13:53:43 UTC+10, Sayth Renshaw  wrote:
> On Thursday, 29 August 2019 13:25:01 UTC+10, Sayth Renshaw  wrote:
> > Hi
> > 
> > Trying to find whats changed in this example. Based around work and team 
> > reschuffles.
> > 
> > So first I created my current teams and then my shuffled teams.
> > 
> > people = ["Tim","Bill","Sally","Ally","Fred","Fredricka"]
> > team_number = [1,1,2,2,3,3]
> > 
> > shuffle_people = ["Fredricka","Bill","Sally","Tim","Ally","Fred"]
> > shuffle_team_number = [1,1,2,2,3,3]
> > 
> > Then combine.
> > 
> > teams = list(zip(people,team_number))
> > shuffle_teams = list(zip(shuffle_people, shuffle_team_number))
> > 
> > Then I am attempting to compare for change.
> > 
> > [i for i, j in zip(teams, shuffle_teams) if i != j]
> > 
> > #Result
> > [('Tim', 1), ('Ally', 2), ('Fred', 3), ('Fredricka', 3)]
> > 
> > #Expecting to see
> > 
> > [('Fredricka', 1),('Tim', 2)]
> > 
> > What's a working way to go about this?
> > 
> > Sayth
> 
> It looks like Tuples are comparing by position changes not content changes.
> 
> So this fails too
> 
> set(shuffle_teams) & set(teams)
> # {('Bill', 1), ('Fred', 3), ('Sally', 2)}

Well this works although its not clear which line is the change.

set(teams).symmetric_difference(set(shuffle_teams))

{('Ally', 2),
 ('Ally', 3), # This is the change Ally changed from 2 to 3
 ('Fredricka', 1), # However here this first line is the change.
 ('Fredricka', 3),
 ('Tim', 1),
 ('Tim', 2)}

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


Re: Compare zip lists where order is important

2019-08-28 Thread Sayth Renshaw
On Thursday, 29 August 2019 13:25:01 UTC+10, Sayth Renshaw  wrote:
> Hi
> 
> Trying to find whats changed in this example. Based around work and team 
> reschuffles.
> 
> So first I created my current teams and then my shuffled teams.
> 
> people = ["Tim","Bill","Sally","Ally","Fred","Fredricka"]
> team_number = [1,1,2,2,3,3]
> 
> shuffle_people = ["Fredricka","Bill","Sally","Tim","Ally","Fred"]
> shuffle_team_number = [1,1,2,2,3,3]
> 
> Then combine.
> 
> teams = list(zip(people,team_number))
> shuffle_teams = list(zip(shuffle_people, shuffle_team_number))
> 
> Then I am attempting to compare for change.
> 
> [i for i, j in zip(teams, shuffle_teams) if i != j]
> 
> #Result
> [('Tim', 1), ('Ally', 2), ('Fred', 3), ('Fredricka', 3)]
> 
> #Expecting to see
> 
> [('Fredricka', 1),('Tim', 2)]
> 
> What's a working way to go about this?
> 
> Sayth

It looks like Tuples are comparing by position changes not content changes.

So this fails too

set(shuffle_teams) & set(teams)
# {('Bill', 1), ('Fred', 3), ('Sally', 2)}
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to use regex to search string between {}?

2019-08-28 Thread Sayth Renshaw


> 
> A site like http://www.pyregex.com/ allows you to check your regex with
> slightly fewer clicks and keystrokes than editing your program.

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


Compare zip lists where order is important

2019-08-28 Thread Sayth Renshaw
Hi

Trying to find whats changed in this example. Based around work and team 
reschuffles.

So first I created my current teams and then my shuffled teams.

people = ["Tim","Bill","Sally","Ally","Fred","Fredricka"]
team_number = [1,1,2,2,3,3]

shuffle_people = ["Fredricka","Bill","Sally","Tim","Ally","Fred"]
shuffle_team_number = [1,1,2,2,3,3]

Then combine.

teams = list(zip(people,team_number))
shuffle_teams = list(zip(shuffle_people, shuffle_team_number))

Then I am attempting to compare for change.

[i for i, j in zip(teams, shuffle_teams) if i != j]

#Result
[('Tim', 1), ('Ally', 2), ('Fred', 3), ('Fredricka', 3)]

#Expecting to see

[('Fredricka', 1),('Tim', 2)]

What's a working way to go about this?

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


Re: Enumerate - int object not subscriptable

2019-08-20 Thread Sayth Renshaw
On Wednesday, 21 August 2019 03:16:01 UTC+10, Ian  wrote:
> Or use the "pairwise" recipe from the itertools docs:
> 
> from itertools import tee
> 
> def pairwise(iterable):
> "s -> (s0,s1), (s1,s2), (s2, s3), ..."
> a, b = tee(iterable)
> next(b, None)
> return zip(a, b)
> 
> for num1, num2 in pairwise(a):
> print(num1, num2)
> 

> > Stanley C. Kitching
> > Human Being
> > Phoenix, Arizona
> >
> > --

This definitely ended up being the ticket.

def pairwise(iterable): 
"s -> (s0,s1), (s1,s2), (s2, s3), ..." 
a, b = tee(iterable) 
next(b, None) 
return zip(a, b) 


def output_data(s):
serie = fibo(input_length)
x = []
y = []

for num1, num2 in pairwise(serie): 
y.append( num2 / num1) 

for item in y:
x.append(y.index(item))

draw_graph(x, y)


Kept trying with the enumerate style
for i, num in enumerate(serie):
while i < len(serie)-1:
x.append(int(serie[i + 1])/ int(serie[i]))
i += 1

but just couldn't quite get it to work. Itertools definitely made it easier.

for i, num in enumerate(serie):
while i < len(serie)-1:
x.append(int(serie[i + 1])/ int(serie[i]))
i += 1
-- 
https://mail.python.org/mailman/listinfo/python-list


Enumerate - int object not subscriptable

2019-08-20 Thread Sayth Renshaw
Hi

I want to do basic math with a list.

a = [1, 2, 3, 4, 5, 6, 7, 8]

for idx, num in enumerate(a): 
print(idx, num)

This works, but say I want to print the item value at the next index as well as 
the current.

for idx, num in enumerate(a): 
print(num[idx + 1], num)

I am expecting 2, 1.

But am receiving 

TypeError: 'int' object is not subscriptable

Why?

Cheers

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


Re: Copying a row from a range of Excel files to another

2019-06-26 Thread Sayth Renshaw
 Cecil Westerhof  wrote:
> I was asked to copy a certain line from about 300 Excel lines to a new
> Excel file. That is not something I would like to do by hand and I
> immediately thought: that should be possible with Python.
> 
> And it is. I was surprised how fast I could write that with openpyxl.
> My first try was not very neat, but a proof of concept. Then by
> looking better at the possibilities I could get much cleaner code. But
> I am still not completely happy. At the moment I have the following
> code:
> wb_out = Workbook()
> for filepath in filepathArr:
> current_row = []
> wb_in   = load_workbook(filepath)
> for cell in wb_in.active[src_row]:
> current_row.append(cell.value)
> wb_out.active.append(current_row)
> wb_in.close()
> wb_out.save(report_start + datetime.now().strftime('%Y-%m-%d') + 
> report_end)
> wb_out.close()
> 
> I could not find a way to copy a row from one workbook to another.
> That is why I put the row in current_row and do an append. Am I
> overlooking something, or is that really the way to do this?
> 
> 
> 
> -- 
> Cecil Westerhof

Pandas may work out better.

import pandas as pd

df = pd.read_excel('spreadsheet')
# find the row by filtering with regex
df2 = df.'column'.str.contains('criteria as regex')
df2.pd.save_excel('output.xlsx')

Cheers

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


Re: for line3 in myips matching too longer matches.

2019-06-26 Thread Sayth Renshaw
Chris Roberts  wrote:
> ###
> CODE:
>elif line1.rstrip(‘\n’) in line2.strip(‘\n’):
>for line3 in myips:
>print “###”
>print “line1 is %s” % line1.rstrip(‘\n’)
>print “line2 is %s” % line2.strip(‘\n’)
> ###
> OUTPUT:
> line1 is 10.10.168.2
> line2 is  - address: 10.10.168.27  # myhost
> ###
> 
> I think the problem is here: 
> line1.rstrip(‘\n’) in line2.strip(‘\n’):  
> 
> I want it to
> match only 10.10.168.2 AND 10.10.168.2:
> NOT match 10.10.168.2[0-9]
> 
> If someone out there knows a simple solution. I would love to see it.
> Thanks in advance. 
> crzzy1

Not sure exactly what the input is but a comprehension would do this.

[x for x in input_line.split(' ') if == '10.10.168.2']

Cheers

Sayth

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


Re: pandas split and melt()

2019-06-26 Thread Sayth Renshaw


> 
> Since I didn't find a cool shortcut I decided to use brute force:
> 
> $ cat pandas_explode_column.py
> import pandas as pd
> 
> df = pd.DataFrame(
> [
> [
> "2019-06-21 11:15:00",
> "WNEWSKI, Joan;#17226;#BALIN, Jock;#18139;#DUNE, Colem;#17230;"
> ],
> [
> "2019-06-22 10:00:00", "Doe, John;#42;Robbins, Rita;"
> ]
> ],
> columns=["Session date", "Consultant"]
> )
> 
> def explode_consultants(consultants):
> consultants = (c.lstrip("#") for c in consultants.split(";"))
> return (c for c in consultants if c.strip("0123456789"))
> 
> def explode_column(df, column, split):
> for _index, row in df.iterrows():
> for part in split(row[column]):
> yield [part if c == column else row[c] for c in df.columns]
> 
> def explode(df, column, split):
> return pd.DataFrame(
> explode_column(df, "Consultant", split), columns=df.columns
> )
> 
> df2 = explode(df, "Consultant", explode_consultants)
> 
> print(df)
> print(df2)
> $ python3 pandas_explode_column.py
>   Session date Consultant
> 0  2019-06-21 11:15:00  WNEWSKI, Joan;#17226;#BALIN, Jock;#18139;#DUNE...
> 1  2019-06-22 10:00:00   Doe, John;#42;Robbins, Rita;
> 
> [2 rows x 2 columns]
>   Session date Consultant
> 0  2019-06-21 11:15:00  WNEWSKI, Joan
> 1  2019-06-21 11:15:00BALIN, Jock
> 2  2019-06-21 11:15:00DUNE, Colem
> 3  2019-06-22 10:00:00  Doe, John
> 4  2019-06-22 10:00:00  Robbins, Rita
> 
> [5 rows x 2 columns]
> $

Mind a little blown :-). Going to have to play and break this several times to 
fully get it. 

Thanks

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


Re: pandas split and melt()

2019-06-26 Thread Sayth Renshaw
Update.

Option 1. - This annihilates all text in the column leaving nothing.

completed_tasks['Consultant'] = 
completed_tasks['Consultant'].str.rstrip('.#123')


Option 2. - returns unhashable series list.

output = 
completed_tasks[completed_tasks['Consultant']].str.contains(r'/\b[^\d\W]+\b/g')

neither option works as the column is a list.

Thanks

Sayth

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


Re: pandas split and melt()

2019-06-26 Thread Sayth Renshaw


> > NB. There are varied amounts of consultants so splitting across columns is 
> > uneven. if it was even melt seems like it would be good 
> > https://dfrieds.com/data-analysis/melt-unpivot-python-pandas
> 
> Keep it simple: https://docs.python.org/3.6/library/string.html
> 
> -- 
> Regards =dn

I have managed to split but not filter the numbers. All other code being the 
same.
...
completed_tasks = df.loc[(df['Completed'] == 'Yes') & (df['Motor/Property'] == 
'Motor') & (df['Delivery Method'] == 'Group Coaching')]
completed_tasks['Consultant'] = completed_tasks['Consultant'].str.split(pat = 
";")
pattern = "\\#d"
completed_tasks['Consultant'] = completed_tasks['Consultant'].str.split(pat = 
";")
completed_tasks['Consultant'] = 
completed_tasks['Consultant'].str.contains(pattern)
...

Works without the regex which causes this error AttributeError: Can only use 
.str accessor with string values, which use np.object_ dtype

pattern = "\\#d"
completed_tasks['Consultant'] = completed_tasks['Consultant'].str.split(pat = 
";")

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


pandas split and melt()

2019-06-26 Thread Sayth Renshaw
Hi

Having fun with pandas filtering a work excel file.
My current script opens selected and filters the data and saves as excel.

import pandas as pd 
import numpy as np

log = pd.read_excel("log_dump_py.xlsx")
df = log.filter(items=['Completed', 'Priority', 'Session date', 'Consultant', 
'Coach',
   'Delivery Method', 'Focus of Coaching', 'Leader', 'Site',
   'Coaching Description','Motor/Property', 
   ],)
completed_tasks = df.loc[(df['Completed'] == 'Yes') & (df['Motor/Property'] == 
'Motor') & (df['Delivery Method'] == 'Group Coaching')]
print(completed_tasks.head(n=5))
completed_tasks.to_excel("filtered_logs.xlsx")

This leaves me with a set of several columns. The main column of concern for 
this example is a consultant

Session dateConsultant
2019-06-21 11:15:00 WNEWSKI, Joan;#17226;#BALIN, Jock;#18139;#DUNE, 
Colem;#17230;

How can I split the consultant column, keep only names and drop the numbers and 
for every session date create a line with data and consultants name?

NB. There are varied amounts of consultants so splitting across columns is 
uneven. if it was even melt seems like it would be good 
https://dfrieds.com/data-analysis/melt-unpivot-python-pandas


Thanks

Sayth


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


Re: generator to write N lines to file

2019-06-24 Thread Sayth Renshaw


> To get the individual lines you have to yield them
> 
>   for line in lines_gen:
>   yield line
> 
> This can be rewritten with some syntactic sugar as
> 
>   yield from lines_gen
>  
> > for line in getWord(fileName, 5):
> > with open(dumpName, 'a') as f:
> > f.write(line)
> 
> Note that getWord as written does not close the infile when you break out of 
> the loop, e. g.
> 
> for line in getWord(filename, 5):
> break
> 
> To avoid that you can use a context manager:
> 
> from itertools import islice
> from contextlib import contextmanager
> 
> infile = ...
> outfile = ...
> 
> @contextmanager
> def head(infile, numlines):
> with open(infile) as f:
> yield islice(f, numlines)
> 
> with open(outfile, "w") as f:
> with head(infile, 5) as lines:
> f.writelines(lines)

Thank You Peter great advice as always.

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


generator to write N lines to file

2019-06-23 Thread Sayth Renshaw
Afternoon

Trying to create a generator to write the first N lines of text to a file.
However, I keep receiving the islice object not the text, why?

from itertools import islice

fileName = dir / "7oldsamr.txt"
dumpName = dir / "dump.txt"

def getWord(infile, lineNum):
with open(infile, 'r+') as f:
lines_gen = islice(f, lineNum)
yield lines_gen

for line in getWord(fileName, 5):
with open(dumpName, 'a') as f:
f.write(line)

Thanks,

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


Re: Function to determine list max without itertools

2019-04-19 Thread Sayth Renshaw


> Now, what happens when the code is tested with various (different) sets 
> of test-data?
> (remember the last question from my previous msg!?)
> 
It fails on any list entry that isn't a float or an int, giving a TypeError.

> Once you are happy with the various test-runs, do you have any ideas for 
> making the code more efficient?
> -- 
> Regards =dn

Efficient No, but resistant to error I can buy using try except to pass through 
TypeErrors.

def max_try(listarg):
myMax = listarg[0]
try:
for item in listarg:
if item > myMax:
myMax = item
except TypeError:
print(f'Only numbers are supported, this entry "{item}" was not')
pass

return myMax


Thanks.

PS Since I am going through the list fully the only optimisation I can think of 
a generator to feed it seems sort of redundant. Unless of course it was a list 
of 10,000 numbers then maybe its useful. 

Maybe a generator with a condition that if list length is greater than 500 to 
chunk it into 250 lengths. 

But then If we go along this path then you could say if every other task in an 
application waited on it could be very important so then async await might be 
an option.

Sayth

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


Re: Function to determine list max without itertools

2019-04-19 Thread Sayth Renshaw
On Friday, 19 April 2019 17:01:33 UTC+10, Sayth Renshaw  wrote:
> Set the first item in the list as the current largest. 
> Compare each subsequent integer to the first.
> if this element is larger, set integer.

def maxitwo(listarg):
myMax = listarg[0]
for item in listarg:
if item > myMax:
myMax = item

return myMax

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


Re: Function to determine list max without itertools

2019-04-19 Thread Sayth Renshaw



  Set the first item in the list as the current largest. 
Compare each subsequent integer to the first.
if this element is larger, set integer. 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Function to determine list max without itertools

2019-04-18 Thread Sayth Renshaw


> 
> In English rather than Python, how do you find the maximum element in a 
> list?
> 
> -- 
> Rob Gaddi, Highland Technology 

Get first 1 item in the list and compare it to the rest. If it is larger than 
rest its the max. However if another list member is larger it replaces the 
first item and comparison continues.

Sayth


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


Re: Function to determine list max without itertools

2019-04-18 Thread Sayth Renshaw


> >
> It's still overly complicated.
> 

This is where I have ended up. Without itertools and max its what I got 
currently.

def maximum(listarg):
myMax = listarg[0]
for item in listarg:
for i in listarg[listarg.index(item)+1:len(listarg)]:
if myMax < i:
myMax = i

return myMax

How would you simplify it?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Function to determine list max without itertools

2019-04-18 Thread Sayth Renshaw
Thank you for the advice everyone.

> 
> The first thing to try is find every place where you update myMax, and

This was actually where I was going wrong. I was setting max but then 
overwriting it with item. Then kept checking item only to return myMax.

I went looking for other solutions as I thought I must be well off the path in 
the shrubs but I was actually close.

This is how I ended up. There may be better solutions but this works.

def maximum(listarg): 
items = list(listarg) 
myMax = items[0] 
for item in items:
for i in items[items.index(item)+1:len(items)]:
if myMax < i:
myMax = i
else:
pass

return myMax
  

if __name__ == "__main__":
print(maximum([4,3,6,2,1,4]))


Cheers

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


Re: Function to determine list max without itertools

2019-04-17 Thread Sayth Renshaw
wrote:
> >
> >
> > I have created a function that takes a list as an argument.
> > Without using itertools I want to compare each item in the list to find the 
> > max.
> >
> > However instead of the max I keep getting the  last item in the list. Where 
> > is my logic wrong here?
> >
> > def maximum(listarg):
> > items = list(listarg)
> > myMax = 0
> > for index, item in enumerate(items):
> > for otheritem in items[index + 1 :]:
> > if item < otheritem:
> > myMax = otheritem
> > elif item > otheritem:
> > myMax = item
> > else:
> > myMax = myMax
> >
> > Seems like it should work but doesn't.
> >
> 
> I'd recommend rethinking your algorithm first, and then thinking about
> debugging your actual code. You should be able to find the largest in
> a collection without making many many passes over the items - a single
> pass should be sufficient.
> 
> ChrisA

Most I am finding either use the max function or itertools. Both of which I am 
trying not to use so that I actually do it myself.

This one on SO is where I was going https://stackoverflow.com/a/3990826/461887

def maxelements(seq):
''' Return list of position(s) of largest element '''
max_indices = []
if seq:
max_val = seq[0]
for i,val in ((i,val) for i,val in enumerate(seq) if val >= max_val):
if val == max_val:
max_indices.append(i)
else:
max_val = val
max_indices = [i]

return max_indices

This is probably the nicest one but still uses Max.

>>> a=[5,4,3,2,1]
>>> def eleMax(items, start=0, end=None):
... return max(items[start:end])

Thanks 

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


Re: Importing module from another subdirectory

2019-04-17 Thread Sayth Renshaw
On Thursday, 18 April 2019 06:59:43 UTC+10, Rich Shepard  wrote:
> What is the proper syntax to import the model class in the model/
> subdirectory into a tkinter view module, e.g., activities.py? The syntax,
> 'import model as m' fails because it is not in the same subdirectory as the
> importing module.
> 
> The program directory tree is:
> 
> bustrac/
> README.rst
> bustrac.py*
> controller/
> model/
> scripts/
> views/
> 
> Running pdb in python3-3.7.3 produces the same error:
> $ python3
> Python 3.7.3 (default, Mar 26 2019, 06:40:28) 
> [GCC 5.5.0] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import pdb
> >>> import activities_data_entry
> Traceback (most recent call last):
>File "", line 1, in 
>File 
> "/home/rshepard/development/business_tracker/views/activities_data_entry.py", 
> line 1, in 
>  import model.activities as act
> ModuleNotFoundError: No module named 'model'
> 
> My previous use of python has had all files in the same directory so I've
> not before had to learn how to address this issue. Pointers appreciated.
> 
> Regards,
> 
> Rich

Morning

Apologies I don't know the answer but went looking. This guide should answer 
the question. Didn't know it was so difficult to be honest.

https://chrisyeh96.github.io/2017/08/08/definitive-guide-python-imports.html#example-directory-structure

Then there was this rather long list of traps. 
http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html

Most guides say it was fixed in 3.3 but still seems quite confusing post 3.3

Cheers

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


Function to determine list max without itertools

2019-04-17 Thread Sayth Renshaw


I have created a function that takes a list as an argument.
Without using itertools I want to compare each item in the list to find the max.

However instead of the max I keep getting the  last item in the list. Where is 
my logic wrong here?

def maximum(listarg):
items = list(listarg)
myMax = 0
for index, item in enumerate(items):
for otheritem in items[index + 1 :]:
if item < otheritem:
myMax = otheritem
elif item > otheritem:
myMax = item
else:
myMax = myMax

Seems like it should work but doesn't.

Cheers

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


Re: hello this ali .. i want some question about python

2019-04-05 Thread Sayth Renshaw
On Saturday, 6 April 2019 08:21:51 UTC+11, maak khan  wrote:
> i need your help guys .. plz

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


Re: I really liked this Javscript FizzBuzz can it be as nice in Python?

2019-04-05 Thread Sayth Renshaw
Wow in both of these examples I have no idea what is happening. Enjoying trying 
to figure it out :-)

> print(*[[n,"Fizz","Buzz","Fizzbuzz"][int("300102100120100"[n%15])] for 
> n in range(1,101)], sep="\n") 

> This is not good code, and if anyone asks, I didn't say you were 
> allowed to do this in an interview. 

Sorry too late that was yesterday, they loved it :-)

> Here's some itertools gymnastics ;)
> 
> from itertools import *
> 
> def make(s, n):
> return cycle(chain(repeat("", n-1), (s,)))
> 
> fizzbuzz = map("".join, zip(make("Fizz", 3), make("Buzz", 5)))
> 
> for i, fb in enumerate(islice(fizzbuzz, 100), 1):
> print(fb or i)

Thanks 

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


I really liked this Javscript FizzBuzz can it be as nice in Python?

2019-04-05 Thread Sayth Renshaw
I saw this fizzbuzz in Eloquent Javascript and thought its really nice. Not all 
the usual if else version, just if.

for (let n = 1; n <= 100; n++) {
  let output = "";
  if (n % 3 == 0) output += "Fizz";
  if (n % 5 == 0) output += "Buzz";
  console.log(output || n);
}

I can't quite get a nice version like this out.

This was my attempt to mimick it. I am sure Python can get it cleaner, but I 
had never thought about not doing if else for Fizzbuzz its just the way I did 
it.

n = range(100)
output = ""
for num in n:
if (num % 3 == 0): output += "Fizz"
if (num % 5 == 0): output += "Buzz"
print(output or num)   

Haven't quite got it. But is possible nice and succinct like the javascript 
version. Maybe lambda will do it, gonna try that.

Any unique mindlowing style FizzBuzz I would never have considered and can 
learn off?

Cheers

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


Re: scalable bottleneck

2019-04-03 Thread Sayth Renshaw
On Thursday, 4 April 2019 10:51:35 UTC+11, Paul Rubin  wrote:
> Sayth Renshaw writes:
> > for x in range ( max_root ):
> > 1) Do you see a memory bottleneck here? If so, what is it?
> > 2) Can you think of a way to fix the memory bottleneck?
> 
> In Python 2, range(n) creates a list in memory.  To make an iterator you
> would use xrange instead of range.  In Python 3, range(n) is an iterator.
> 
> Either way, fetch_squares creates a list of squares and not an iterator.
> 
> > for square in fetch_squares (MAX ):
> > do_something_with ( square )
> 
> Here's you're creating that list and throwing it away.  So you have the
> right idea with:
> 
> def supply_squares(max_root):
> for x in max_root:
> yield x
> 
> But supply_squares returns a generator which means you have to iterate
> through it.
> 
> print(supply_squares(item))
> 
> will print something like
> 
>
> 
> which is what you saw.  You want:
> 
>for square in supply_squares(MAX):
>   print square

Ah thanks for your help. Happy I identified the correct bottleneck too. 

Cheers

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


scalable bottleneck

2019-04-03 Thread Sayth Renshaw
In an email, I received this question as part of a newsletter.

def fetch_squares ( max_root ):
squares = []
for x in range ( max_root ):
squares . append (x **2)
return squares

MAX = 5

for square in fetch_squares (MAX ):
 do_something_with ( square )

1) Do you see a memory bottleneck here? If so, what is it?
2) Can you think of a way to fix the memory bottleneck?

Want to know if I am trying to solve the correct bottleneck.
I am thinking that the bottleneck is to create a list only to iterate the same 
list you created sort of doubling the time through.

Is that the correct problem to solve?

If it is then I thought the best way is just to supply the numbers on the fly, 
a generator.

def supply_squares(max_root):
for x in max_root:
yield x

MAX = 5


So then I set up a loop and do whatever is needed. At this time I am creating 
generator objects. But is this the correct way to go? More of a am I thinking 
correctly questino.

item = 0
while item < MAX:
print(supply_squares(item))
item += 1







Thanks

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


Re: Implement C's Switch in Python 3

2019-02-02 Thread Sayth Renshaw
On Sunday, 3 February 2019 14:31:14 UTC+11, Avi Gross  wrote:
> Yes, you caught the usual flaw in the often irregular English language.
> 
> The 11th, 12th and 13th do all begin with 1 so there is a simple fix in the
> function version by checking if day//10 is 1.
> 
> Revised example:
> 
> """ Use last digit to determine suffix handling teens well """
> 
> def nthSuffix(day):
> if (day // 10 == 1):
> suffix = "th"
> else:
> nth = day % 10
> suffix = "st" if nth == 1 else ("nd" if nth == 2 else ("rd" if nth
> == 3 else "th"))
> 
> return str(day) + suffix
> 
> >>> [ nthSuffix(day) for day in range(1,32)]
> ['1st', '2nd', '3rd', '4th', '5th', '6th', '7th', '8th', '9th', '10th',
> '11th', '12th', '13th', '14th', '15th', '16th', '17th', '18th', '19th',
> '20th', '21st', '22nd', '23rd', '24th', '25th', '26th', '27th', '28th',
> '29th', '30th', '31st']
> 
> Want a dictionary version? Use the above to make a full dictionary:
> 
> >>> chooseFrom = { day : nthSuffix(day) for day in range(1,32)}
> >>> chooseFrom
> {1: '1st', 2: '2nd', 3: '3rd', 4: '4th', 5: '5th', 6: '6th', 7: '7th', 8:
> '8th', 9: '9th', 10: '10th', 11: '11th', 12: '12th', 13: '13th', 14: '14th',
> 15: '15th', 16: '16th', 17: '17th', 18: '18th', 19: '19th', 20: '20th', 21:
> '21st', 22: '22nd', 23: '23rd', 24: '24th', 25: '25th', 26: '26th', 27:
> '27th', 28: '28th', 29: '29th', 30: '30th', 31: '31st'}
> >>> chooseFrom[1]
> '1st'
> >>> chooseFrom[11]
> '11th'
> >>> chooseFrom[21]
> '21st'
> 


> Subject: Re: Implement C's Switch in Python 3
> 
> On 2019-02-03 02:51, Avi Gross wrote:
> > I may be missing something, but the focus seems to be only on the 
> > rightmost digit. You can get that with
> > 
> I had the same thought, but came across a problem. "11st", "12nd", "13rd"?
> 
> [snip]
> > 
> > Output:
> > 
> >>>> for day in range(1, 32):
> > print( nthSuffix(day))
> > 
> > 1st
> > 2nd
> > 3rd
> > 4th
> > 5th
> > 6th
> > 7th
> > 8th
> > 9th
> > 10th
> > 11st
> > 12nd
> > 13rd
> > 14th
> > 15th
> > 16th
> > 17th
> > 18th
> > 19th
> > 20th
> > 21st
> > 22nd
> > 23rd
> > 24th
> > 25th
> > 26th
> > 27th
> > 28th
> > 29th
> > 30th
> > 31st
> > 
> [snip]
> --

I do like this.

Want a dictionary version? Use the above to make a full dictionary: 

>>> chooseFrom = { day : nthSuffix(day) for day in range(1,32)} 
>>> chooseFrom 
{1: '1st', 2: '2nd', 3: '3rd', 4: '4th', 5: '5th', 6: '6th', 7: '7th', 8: 
'8th', 9: '9th', 10: '10th', 11: '11th', 12: '12th', 13: '13th', 14: '14th', 
15: '15th', 16: '16th', 17: '17th', 18: '18th', 19: '19th', 20: '20th', 21: 
'21st', 22: '22nd', 23: '23rd', 24: '24th', 25: '25th', 26: '26th', 27: 
'27th', 28: '28th', 29: '29th', 30: '30th', 31: '31st'} 
>>> chooseFrom[1] 
'1st' 
>>> chooseFrom[11] 
'11th' 
>>> chooseFrom[21] 
'21st' 

Not having a default case as in switch forced you to write out all possible 
combinations.

I think the intent and readbility of switch statements is a bit nicer. 

Cheers

Sayth

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


Re: Implement C's Switch in Python 3

2019-02-02 Thread Sayth Renshaw


> >I am trying to convert a switch statement from C into Python. (why? 
> >practising).
> >
> >This is the C code.
> >
> >printf("Dated this %d", day);
> >  switch (day) {
> >case 1: case 21: case 31:
> >printf("st"); break;
> >case 2: case 22:
> >printf("nd"); break;
> >case 3: case 23:
> >printf("rd"); break;
> >default: printf("th"); break;
> >
> >  }
> >  printf(" day of ");
> >
> >#Premise if the use enter an int as the date 21 for example it would print 
> >21st. It appends the correct suffix onto a date.
> >Reading and trying to implement a function that uses a dictionary. Not 
> >sure how to supply list into it to keep it brief and with default case 
> >of 'th'.
> >
> >This is my current code.
> >
> >def f(x):
> >return {
> >[1, 21, 31]: "st",
> >[2, 22]: "nd",
> >[3, 23]: "rd",
> >}.get(x, "th")
> >
> >
> >print(f(21))
> >
> >I have an unhashable type list. Whats the best way to go?
> 
> Skip has commented on lists being unhashable. We can elaborate on that 
> if you like.
> 
> However, even if you went to tuples (which would let you construct the 
> dict you lay out above), there is another problem.
> 
> You're looking up "x" in the dict. But the keys of the dict are not 
> integers, they are lists (or tuples) or integers, so they won't match.
> 
> You _could_ do this:
> 
>   return {
> 1: "st", 21: "st", 31: "st",
> 2: "nd", 22: "nd",
> 3: "rd", 23: "rd",
>   }.get(x, "th")
> 
> which makes distinct entries for each integer value of interest.
> 
> The conventional approach would normally be:
> 
>   if x in (1, 21, 31):
> return "st"
>   if x in (2, 22):
> return "nd"
>   if x in (3, 23):
> return "rd"
>   return "th"
> 
> While this works for a small number of choices, if you had a huge dict 
> with lots of possible values you could return to your 
> dict-keyed-on-tuples approach. You would need to try each tuple in turn:
> 
>   mapping = {
> (1, 21, 31): "st",
> (2, 22): "nd",
> (3, 23): "rd",
>   }
>   for key, suffix in mapping.items():
> if x in key:
>   return suffix
>   return "th"
> 
> However, for big dictionaries (with many keys) you loose a key strength 
> of dicts: constant time lookup. You can see the above code (and the 
> earlier "if" code) are rather linear, with run time going up linearly 
> with the number of keys. You're better with the int->string single value 
> dict version.
> 
> Cheers,
> Cameron Simpson 

It seems odd with C having switch that its cleaner and more efficient than 
python where we are having to implement our own functions to recreate switch 
everytime.

Or perhaps use a 3rd party library like 
https://github.com/mikeckennedy/python-switch

You have both given good options, it seems there are no standard approaches in 
this case.

Cheers

Sayth

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


Implement C's Switch in Python 3

2019-02-02 Thread Sayth Renshaw
Hi

I am trying to convert a switch statement from C into Python. (why? practising).

This is the C code. 

printf("Dated this %d", day);
  switch (day) {
case 1: case 21: case 31:
printf("st"); break;
case 2: case 22:
printf("nd"); break;
case 3: case 23:
printf("rd"); break;
default: printf("th"); break;

  }
  printf(" day of ");

#Premise if the use enter an int as the date 21 for example it would print 
21st. It appends the correct suffix onto a date.

Reading and trying to implement a function that uses a dictionary. Not sure how 
to supply list into it to keep it brief and with default case of 'th'.

This is my current code.

def f(x):
return {
[1, 21, 31]: "st",
[2, 22]: "nd",
[3, 23]: "rd",
}.get(x, "th")


print(f(21))

I have an unhashable type list. Whats the best way to go?

Cheers

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


Re: Use a function arg in soup

2018-08-01 Thread Sayth Renshaw
Thanks Peter

###
(2) attrs is a dict, so iterating over it will lose the values. Are you sure 
you want that? 
###

Yes initially I want just the keys as a list so I can choose to filter them out 
to only the ones I want. 

# getAttr
Thanks very much will get my function up and working. 

Cheers

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


Use a function arg in soup

2018-08-01 Thread Sayth Renshaw
Hi.

I want to use a function argument as an argument to a bs4 search for attributes.

I had this working not as a function
   # noms = soup.findAll('nomination')
# nom_attrs = []
# for attr in soup.nomination.attrs:
# nom_attrs.append(attr)
But as I wanted to keep finding other elements I thought I would turn it into a 
generator function.


def attrKey(element):
for attr in soup.element.attrs:
yield attr

results in a Nonetype as soup.element.attrs is passed and the attribute isn't 
substituted.

# Attempt 2 
def attrKey(element):
for attr in "soup.{}.attrs".format(element):
yield attr

# Attempt 3 
def attrKey(element):
search_attr = "soup.{}.attrs".format(element)
for attr in search_attr:
yield attr


so I would call it like
attrKey('nomination')

Any ideas on how the function argument can be used as the search attribute?

Thanks

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


Re: Format list of list sub elements keeping structure.

2018-07-24 Thread Sayth Renshaw
> 
> Well, your code was close. All you needed was a little tweak
> to make it work like you requested. So keep working at it,
> and if you have a specific question, feel free to ask on the
> list.
> 
> Here's a tip. Try to simplify the problem. Instead of
> looping over a list of lists, and then attempting to do a
> format in the middle of an iteration, a format that you
> really don't know how to do in a vacuum (no pressure,
> right???), pull one of the sublists out and try to format it
> _first_. IOWs: isolate the problem.
> 
> And, when you can format _one_ list the way you want --
> spoiler alert! -- you can format an infinite number of lists
> the way you want. Loops are cool like that. Well, most of
> the time.
> 
> The key to solving most complex problems is to (1) break
> them down into small parts, (2) solve each small part, and
> (3) assemble the whole puzzle. This is a skill you must
> master. And it's really not difficult. It just requires a
> different way of thinking about tasks.

Thank you Rick, good advice. I really am enjoying coding at the moment, got 
myself and life in a good headspace.

Cheers

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


Re: Format list of list sub elements keeping structure.

2018-07-24 Thread Sayth Renshaw


> > Then using this cool answer on SO [...]
> 
> Oh. I thought you wanted to learn how to solve problems. I had no idea you 
> were auditioning for the James Dean part. My bad.

Awesome response burn lol.

I am trying to solve problems. Getting tired of dealing with JSON and having to 
figure out the structure each time. Just want to automate that part so I can 
move through the munging part and spend more time on higher value tasks.

Cheers

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


Re: Format list of list sub elements keeping structure.

2018-07-24 Thread Sayth Renshaw


> myjson = ...
> path = "['foo']['bar'][42]"
> print(eval("myjson" + path))
> 
> ?
> 
> Wouldn't it be better to keep 'data' as is and use a helper function like
> 
> def get_value(myjson, path):
> for key_or_index in path:
> myjson = myjson[key_or_index]
> return myjson
> 
> path = ['foo', 'bar', 42]
> print(get_value(myjson, path))
> 
> ?

Currently I do leave the data I extract the keys out as a full path.

If I use pprint as suggested I get close.

   ['glossary'],
['glossary', 'title'],
['glossary', 'GlossDiv'],
['glossary', 'GlossDiv', 'title'],
['glossary', 'GlossDiv', 'GlossList'],
['glossary', 'GlossDiv', 'GlossList', 'GlossEntry'],
['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'ID'],
['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'SortAs'],
['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossTerm'],
['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'Acronym'],
...]

But to select elements from the json I need the format json['elem1']['elem2] .

I want to be able to get any json in future and parse it into my function and 
return a list of all json key elements.

Then using this cool answer on SO https://stackoverflow.com/a/14692747/461887

from functools import reduce  # forward compatibility for Python 3
import operator

def getFromDict(dataDict, mapList):
return reduce(operator.getitem, mapList, dataDict)

def setInDict(dataDict, mapList, value):
getFromDict(dataDict, mapList[:-1])[mapList[-1]] = value

Then get the values from the keys
>>> getFromDict(dataDict, ["a", "r"])
1


That would mean I could using my function if I get it write be able to feed it 
any json, get all the full paths nicely printed and then feed it back to the SO 
formula and get the values.

It would essentially self process itself and let me get a summary of all keys 
and their data.

Thanks 

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


Re: Format list of list sub elements keeping structure.

2018-07-23 Thread Sayth Renshaw
On Tuesday, 24 July 2018 14:25:48 UTC+10, Rick Johnson  wrote:
> Sayth Renshaw wrote:
> 
> > elements = [['[{0}]'.format(element) for element in elements]for elements 
> > in data]
> 
> I would suggest you avoid list comprehensions until you master long-form 
> loops.

I actually have the answer except for a glitch where on list element is an int.

My code

for item in data:
out = '[{0}]'.format("][".join(item))
print(out)

which prints out

[glossary]
[glossary][title]
[glossary][GlossDiv]
[glossary][GlossDiv][title]
[glossary][GlossDiv][GlossList]


However, in my source I have two lines like this

 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossDef', 
'GlossSeeAlso', 0],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossDef', 
'GlossSeeAlso', 1],

when it hits these lines I get 

TypeError: sequence item 6: expected str instance, int found

Do I need to do an explicit check for these 2 cases or is there a simpler way?

Cheers

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


Re: Format list of list sub elements keeping structure.

2018-07-23 Thread Sayth Renshaw
On Tuesday, 24 July 2018 14:25:48 UTC+10, Rick Johnson  wrote:
> Sayth Renshaw wrote:
> 
> > elements = [['[{0}]'.format(element) for element in elements]for elements 
> > in data]
> 
> I would suggest you avoid list comprehensions until you master long-form 
> loops.

My general issue is that I want to pick up all the elements in each sublist and 
operate on them to concatenate them together.

However, using for loops I get each list then each element of each list.
When in my head I want each list then all elements of each list.

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


Re: Format list of list sub elements keeping structure.

2018-07-23 Thread Sayth Renshaw
I am very close to the end result. I now have it as

Output
[   ['[glossary]'],
['[glossary]', '[title]'],
['[glossary]', '[GlossDiv]'],
['[glossary]', '[GlossDiv]', '[title]'],
['[glossary]', '[GlossDiv]', '[GlossList]'],
['[glossary]', '[GlossDiv]', '[GlossList]', '[GlossEntry]'],
.]

I used.

elements = [['[{0}]'.format(element) for element in elements]for elements in 
data]

Is there a good way to strip the ', ' and make the list a list of 1 element 
lists? Thoughts on achieving this?

So

[   ['[glossary]'],
['[glossary][title]'],
]

Cheers

Sayth

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


Re: Format list of list sub elements keeping structure.

2018-07-23 Thread Sayth Renshaw


> > 
> > for item in data:
> > for elem in item:
> > out = ("[{0}]").format(elem)
> > print(out)
> 
> Hint: print implicitly adds a newline to the output string. So collect all 
> the values of each sublist and print a line-at-time to output, or use the 
> end= argument of Py3's print, or find another solution. Also remember that 
> indention is significant in Python.

Thanks

Getting closer.

answer = []
for item in data:
for elem in item:
out = ("[{0}]").format(elem)
answer.append(out)

print(answer)

Think I need to bring it in a list not an element of a list and process it.

Cheers

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


Format list of list sub elements keeping structure.

2018-07-23 Thread Sayth Renshaw
I have data which is a list of lists of all the full paths in a json document.

How can I change the format to be usable when selecting elements?

data = [['glossary'],
 ['glossary', 'title'],
 ['glossary', 'GlossDiv'],
 ['glossary', 'GlossDiv', 'title'],
 ['glossary', 'GlossDiv', 'GlossList'],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry'],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'ID'],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'SortAs'],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossTerm'],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'Acronym'],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'Abbrev'],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossDef'],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossDef', 'para'],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossDef', 
'GlossSeeAlso'],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossDef', 
'GlossSeeAlso', 0],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossDef', 
'GlossSeeAlso', 1],
 ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'GlossSee']]

I am trying to change it to be.

[['glossary'],
 ['glossary']['title'],
 ['glossary']['GlossDiv'],
]

Currently when I am formatting I am flattening the structure(accidentally).

for item in data:
for elem in item:
out = ("[{0}]").format(elem)
print(out)

Which gives

[glossary]
[title]
[GlossDiv]
[title]
[GlossList]
[GlossEntry]
[ID]
[SortAs]
[GlossTerm]
[Acronym]
[Abbrev]
[GlossDef]
[para]
[GlossSeeAlso]
[0]
[1]
[GlossSee]


Cheers

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


Re: Data Integrity Parsing json

2018-04-25 Thread Sayth Renshaw
On Thursday, 26 April 2018 07:57:28 UTC+10, Paul Rubin  wrote:
> Sayth Renshaw  writes:
> > What I am trying to figure out is how I give myself surety that the
> > data I parse out is correct or will fail in an expected way.
> 
> JSON is messier than people think.  Here's an article with some
> explanation and test code:
> 
>   http://seriot.ch/parsing_json.php

Thanks Paul there is a lot of good information in that article.

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


Re: Looping on a list in json

2017-11-04 Thread Sayth Renshaw


no doubt tho after playing with this is that enumerate value ends up in the 
output which is a dictionary. The enumerate has no key which makes it invalid 
json if dumped.  

Not massive issue but getting the effect of enumerate without polluting output 
would be the winner.


>runner_lists = {} 
>for n, item in enumerate(result): 
># if this one is interested / not -filtered: 
>print(n, item) 
>runner_lists[n] = result[n]["RacingFormGuide"]["Event"]["Runners"] 

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


Re: Looping on a list in json

2017-11-04 Thread Sayth Renshaw
Sorry

figured it. Needed to use n to iterate when creating.

runner_lists = {}
for n, item in enumerate(result):
# if this one is interested / not -filtered:
print(n, item)
runner_lists[n] = result[n]["RacingFormGuide"]["Event"]["Runners"]

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


Re: Looping on a list in json

2017-11-04 Thread Sayth Renshaw

> I'd just keep the interesting runners, along with their race numbers, in a 
> dict. The enumerate function is handy here. Something like (untested):
> 
>   runner_lists = {}
>   for n, item in enumerate(result):
> if this one is interested/not-filtered:
>   runner_lists[n] = result["RacingFormGuide"]["Event"]["Runners"]
> 
> and just return runner_lists. That way you know what the race numbers were 
> for 
> each list of runners.
> 

> 
> Cheers,
> Cameron Simpson 

The main issue is that enumerate doesn't enumerate on the lists when trying to 
filter.

result = meeting_id["Races"]

so yes enumerating this works, showing just printing n and item.

runner_lists = {}
for n, item in enumerate(result):
# if this one is interested / not -filtered:
print(n, item)

0 {'FeatureRaceBonusActive': 'Disabled', 'FixedPriceSummary': {'FixedPrices': 
[{'SportId': 8, 'LeagueId': 102, 'MeetingId': 1218, 'MainEventId': 650350, 
'SubEventId': 3601361, 'Status': 'F', 'StatusDescription': 'FINALISED', 
'BetTypeName': 'Win', 'EnablePlaceBetting': True}]}, 'RacingFormGuide': 
{'Copyright': .

and so on it goes through the 7 items in this file.

but including 

runner_lists = {}
for n, item in enumerate(result):
# if this one is interested / not -filtered:
print(n, item)
runner_lists[n] = result["RacingFormGuide"]["Event"]["Runners"]

## Produces

Traceback (most recent call last):
dict_keys(['RaceDay', 'ErrorInfo', 'Success'])
  File "/home/sayth/PycharmProjects/ubet_api_mongo/parse_json.py", line 31, in 

runner_lists[n] = result["RacingFormGuide"]["Event"]["Runners"]
TypeError: list indices must be integers or slices, not str


Cheers

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


Re: Read Firefox sqlite files with Python

2017-11-04 Thread Sayth Renshaw
On Sunday, 5 November 2017 04:32:26 UTC+11, Steve D'Aprano  wrote:
> I'm trying to dump a Firefox IndexDB sqlite file to text using Python 3.5.
> 
> 
> import sqlite3
> con = sqlite3.connect('foo.sqlite')
> with open('dump.sql', 'w') as f:
> for line in con.iterdump():
> f.write(line + '\n')
> 
> 
> The error I get is:
> 
> Traceback (most recent call last):
>   File "", line 2, in 
>   File "/usr/local/lib/python3.5/sqlite3/dump.py", line 30, in _iterdump
> schema_res = cu.execute(q)
> sqlite3.DatabaseError: file is encrypted or is not a database
> 
> 
> If I open the file in a hex editor, it starts with:
> 
> SQLite format 3
> 
> and although I can see a few human readable words, the bulk of the file looks
> like noise.
> 
> 
> 
> 
> -- 
> Steve
> “Cheer up,” they said, “things could be worse.” So I cheered up, and sure
> enough, things got worse.

https://stackoverflow.com/a/18601429

Version mismatch between sqlite CLI and python sqlite API? I created again my 
db from the script instead of the CLI. Now insert and select work from the 
script, but not from the CLI. $sqlite -version returns 2.8.17, while the python 
version is 2.7.3.

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


Re: Looping on a list in json

2017-11-04 Thread Sayth Renshaw
On Sunday, 5 November 2017 09:53:37 UTC+11, Cameron Simpson  wrote:

> >I want to get a result from a largish json api. One section of the json 
> >structure returns lists of data. I am wanting to get each resulting list 
> >returned.
> >
> >This is my code.
> >import json
> >from pprint import pprint
> >
> >with open(r'/home/sayth/Projects/results/Canterbury_2017-01-20.json', 'rb') 
> >as f, open('socks3.json','w') as outfile:
> >to_read = json.load(f)
> [...]
> >meeting_id = to_read["RaceDay"]["Meetings"][0]
> >result = meeting_id["Races"]
> >#failing
> >for item in result:
> >pprint(["RacingFormGuide"]["Event"]["Runners"])
> 
> I'd just keep the interesting runners, along with their race numbers, in a 
> dict. The enumerate function is handy here. Something like (untested):
> 
>   runner_lists = {}
>   for n, item in enumerate(result):
> if this one is interested/not-filtered:
>   runner_lists[n] = result["RacingFormGuide"]["Event"]["Runners"]
> 
> and just return runner_lists. That way you know what the race numbers were 
> for 
> each list of runners.
> 
> >What is the best way to and return the data?
> 
> The basic idea is to make a small data structure of your own (just the 
> dictionary runner_lists in the example above) and fill it in with the 
> infomation you care about in a convenient and useful shape. Then just return 
> the data structure.
> 
> The actual data structure will depend on what you need to do with this later.
> 
> Cheers,


Thank you. That does seem a good approach. 

I was intending to merge other dictionary data from other dicts within the json 
structure and that's where the trouble starts i guess trying to get too much 
from json.

Thanks

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


Looping on a list in json

2017-11-04 Thread Sayth Renshaw
Hi

I want to get a result from a largish json api. One section of the json 
structure returns lists of data. I am wanting to get each resulting list 
returned.

This is my code.
import json
from pprint import pprint

with open(r'/home/sayth/Projects/results/Canterbury_2017-01-20.json', 'rb') as 
f, open('socks3.json','w') as outfile:
to_read = json.load(f)


print(to_read.keys())
# pprint(to_read)
meet = to_read["RaceDay"]["Meetings"]
meeting_id = to_read["RaceDay"]["Meetings"][0]
pprint(meeting_id.keys())
# result = meeting_id["Races"][1]["RacingFormGuide"]["Event"]["Runners"]
result = meeting_id["Races"]
#failing
for item in result:
pprint(["RacingFormGuide"]["Event"]["Runners"])


The key to the issue is that
result = meeting_id["Races"][0]["RacingFormGuide"]["Event"]["Runners"]
result = meeting_id["Races"][1]["RacingFormGuide"]["Event"]["Runners"]
result = meeting_id["Races"][2]["RacingFormGuide"]["Event"]["Runners"]

the numbers though in the above results could go from 0 to 10.

What is the best way to and return the data?
 would just save meeting_id["Races"] to my result however there are a lot of 
other junk dictionaries and lists I am filtering.

Cheers

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


Re: pathlib PurePosixPath

2017-10-10 Thread Sayth Renshaw

> > Hi
> >
> > How do I create a valid file name and directory with pathlib?
> >
> > When I create it using PurePosixPath I end up with an OSError due to an 
> > obvously invlaid path being created.
> 
> You're on Windows. The rules for POSIX paths don't apply to your file
> system, and...
> 
> > OSError: [Errno 22] Invalid argument: 
> > 'C:\\Users\\Sayth\\Projects\\results/Warwick Farm2017-09-06T00:00:00.json'
> 
> ... the colon is invalid on Windows file systems. You'll have to
> replace those with something else.
> 
> ChrisA

Thanks. Updated the script. But shouldn't it create the file if it doesn't 
exist? Which none of them will.

for dates in fullUrl:
# print(dates)
time.sleep(0.3)
r = requests.get(dates)
data = r.json()
if data["RaceDay"] is not None:
a = data["RaceDay"]["MeetingDate"]
b = a[:7]
file_name = data["RaceDay"]["Meetings"][0]["VenueName"] + '_' + b + 
'.json'
result_path = pathlib.PurePath(r'C:\Users\Sayth\Projects\results', 
file_name)
with open(result_path, 'a') as f:
f.write(data)

##Output
File "C:/Users/Sayth/PycharmProjects/ubet_api_mongo/json_download.py", line 42, 
in 
with open(result_path, 'a') as f:
FileNotFoundError: [Errno 2] No such file or directory: 
'C:\\Users\\Sayth\\Projects\\results\\Warwick Farm_2017-09.json'

Process finished with exit code 1

Cheers

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


pathlib PurePosixPath

2017-10-10 Thread Sayth Renshaw
Hi

How do I create a valid file name and directory with pathlib?

When I create it using PurePosixPath I end up with an OSError due to an 
obvously invlaid path being created.

import pathlib

for dates in fullUrl:
# print(dates)
time.sleep(0.3)
r = requests.get(dates)
data = r.json()
if data["RaceDay"] is not None:
file_name = data["RaceDay"]["Meetings"][0]["VenueName"] + 
data["RaceDay"]["MeetingDate"] + '.json'
result_path = pathlib.PurePosixPath(r'C:\Users\Sayth\Projects\results', 
file_name)
with open(result_path, 'a') as f:
f.write(data)


##Output
C:\Users\Sayth\Anaconda3\envs\json\python.exe 
C:/Users/Sayth/PycharmProjects/ubet_api_mongo/json_download.py
Traceback (most recent call last):
  File "C:/Users/Sayth/PycharmProjects/ubet_api_mongo/json_download.py", line 
40, in 
with open(result_path, 'a') as f:
OSError: [Errno 22] Invalid argument: 
'C:\\Users\\Sayth\\Projects\\results/Warwick Farm2017-09-06T00:00:00.json'

Process finished with exit code 1

Not sure exactly which way to fix it.

Cheers

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


Re: Suggestions on storing, caching, querying json

2017-10-06 Thread Sayth Renshaw
On Thursday, 5 October 2017 15:13:43 UTC+11, Sayth Renshaw  wrote:
> HI
> 
> Looking for suggestions around json libraries. with Python. I am looking for 
> suggestions around a long term solution to store and query json documents 
> across many files.
> 
> I will be accessing an api and downloading approx 20 json files from an api a 
> week. Having downloaded this year I have over 200 files already. So it will 
> grow at a reasonable rate.
> 
> What I have initially done is store them into a mongo db. Now I am wondering 
> if this is useful or prudent since other than querying the json I wont have 
> much use of other mongo features.
> 
> When querying the json files though queries will utilise multiple json files 
> at once, not just retrieving a single record. The usage is for data analysis.
> 
> Is there a good json storage option, with caching and optimal querying etc.
> 
> Regarding querying I did find a library for json searching called ObjectPath 
> written in Python http://objectpath.org/reference.html
> 
> Looking to leverage your experience.
> 
> Cheers
> 
> Sayth

There is a new extension for redis ReJson and redis-py for using redis and 
python as a json store. http://rejson.io/ and 
https://github.com/andymccurdy/redis-py. Not sure if this has much more upside 
than mongo other than having a more fmailiar query language like JsonPath 
http://rejson.io/path/

Cheers

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


Suggestions on storing, caching, querying json

2017-10-04 Thread Sayth Renshaw
HI

Looking for suggestions around json libraries. with Python. I am looking for 
suggestions around a long term solution to store and query json documents 
across many files.

I will be accessing an api and downloading approx 20 json files from an api a 
week. Having downloaded this year I have over 200 files already. So it will 
grow at a reasonable rate.

What I have initially done is store them into a mongo db. Now I am wondering if 
this is useful or prudent since other than querying the json I wont have much 
use of other mongo features.

When querying the json files though queries will utilise multiple json files at 
once, not just retrieving a single record. The usage is for data analysis.

Is there a good json storage option, with caching and optimal querying etc.

Regarding querying I did find a library for json searching called ObjectPath 
written in Python http://objectpath.org/reference.html

Looking to leverage your experience.

Cheers

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


Re: None is None but not working

2017-09-28 Thread Sayth Renshaw
Thank you it was data["RaceDay"] that was needed.

ata = r.json()
if data["RaceDay"] is None:
print("Nothing here")
else:
print(data["RaceDay"])


Nothing here
Nothing here
Nothing here
{'MeetingDate': '2017-01-11T00:00:00', .

Thanks

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


None is None but not working

2017-09-27 Thread Sayth Renshaw
Hi

I have got a successful script setup to rotate through dates and download json 
data from the url.

As the api returns 200 whether successful I want to check if the file returned 
is not successful.

when a file doesn't exist the api returns
{'RaceDay': None, 'ErrorInfo': {'SystemId': 200, 'ErrorNo': 55013, 
'DisplayMessage': 'File Not Found.', 'ContactSupport': False, 
'SupportErrorReference': '200-55013'}, 'Success': False}

When I call data = r.json() it says its type is None if it is not successful so 
I thought it easier to check that.

However checking for None does not work the flow in my if else falls straight 
to else.

for dates in fullUrl:
r = requests.get(dates)
data = r.json()
if data is None:
print("Nothing here")
else:
print(data["RaceDay"])

and I get output of

None
None
{'MeetingDate': '2017-01- ... and so on.

How can I actually get this to check?

If i use type(data) I also get None.

Cheers

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


Re: Easy way to get a list of tuples.

2017-09-21 Thread Sayth Renshaw

> >
> >  Thanks Thomas yes you are right with append. I have tried it but just 
> > can't get it yet as append takes only 1 argument and I wish to give it 3.
> >
> You have not showed us what you tried, but you are probably missing a pair 
> of brackets.
> 
> C:\Users\User>python
> Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit 
> (AMD64)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
> >>> x = []
> >>> x.append(('a', 'b', 'c'))
> >>> x.append(('p', 'q', 'r'))
> >>> x
> [('a', 'b', 'c'), ('p', 'q', 'r')]
> >>>
> 
> Does this help?
> 
> Frank Millman

Oh yes I just had one set of brackets with my append.

Thanks

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


Re: Easy way to get a list of tuples.

2017-09-21 Thread Sayth Renshaw
On Thursday, 21 September 2017 20:31:28 UTC+10, Thomas Jollans  wrote:
> On 2017-09-21 12:18, Sayth Renshaw wrote:
> > This is my closest code
> > 
> > data = r.json()
> > 
> > raceData = []
> > 
> > for item in data["RaceDay"]['Meetings'][0]['Races']:
> > raceDetails = item['RacingFormGuide']['Event']['Race']
> > raceData += 
> > (raceDetails['Name'],raceDetails['Number'],raceDetails['Distance'])
> > 
> > print(raceDetails)
> > 
> 
> You're close!
> 
> The operator += extends a list with the items of another sequence (or
> iterable). What you're looking for is the method .append(), which adds a
> single element.
> 
> Observe:
> 
> Python 3.6.0 |Continuum Analytics, Inc.| (default, Dec 23 2016, 12:22:00)
> [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> py> a_list = []
> py> a_list += 1,2,3
> py> a_list
> [1, 2, 3]
> py> a_list.append(4)
> py> a_list
> [1, 2, 3, 4]
> py> a_list += 4
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: 'int' object is not iterable
> py> a_list.append((5,6,7))
> py> a_list
> [1, 2, 3, 4, (5, 6, 7)]
> py>
> 
> 
> -- 
> Thomas Jollans

Thanks Thomas yes you are right with append. I have tried it but just can't get 
it yet as append takes only 1 argument and I wish to give it 3.

I am really having trouble creating the groups of 3, since I am getting one 
consistent stream.

Cheers

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


Easy way to get a list of tuples.

2017-09-21 Thread Sayth Renshaw
Hi

I have been toying with json and I particular area where I cannot get the 
desired result a list of tuples as my return. The json from the API is way to 
long but I don't think it will matter.

.. hitting url 
data = r.json()

for item in data["RaceDay"]['Meetings'][0]['Races']:
raceDetails = item['RacingFormGuide']['Event']['Race']
print(raceDetails)

This returns

{'Number': 1, 'NumberDisplay': '01', 'Distance': 1000, 'DistanceDisplay': '1000 
METRES', 'Name': 'CLASS 3 HANDICAP', 'NameForm': 'HIGHWAY-C3'}
{'Number': 2, 'NumberDisplay': '02', 'Distance': 1600, 'DistanceDisplay': '1600 
METRES', 'Name': 'BM 90 HANDICAP', 'NameForm': 'BM90'}
{'Number': 3, 'NumberDisplay': '03', 'Distance': 1100, 'DistanceDisplay': '1100 
METRES', 'Name': 'HERITAGE STAKES', 'NameForm': 'HERITAGE'}
{'Number': 4, 'NumberDisplay': '04', 'Distance': 1400, 'DistanceDisplay': '1400 
METRES', 'Name': 'BILL RITCHIE HANDICAP', 'NameForm': 'RITCHIE'}
{'Number': 5, 'NumberDisplay': '05', 'Distance': 1400, 'DistanceDisplay': '1400 
METRES', 'Name': 'TEA ROSE STAKES', 'NameForm': 'TEA ROSE'}
{'Number': 6, 'NumberDisplay': '06', 'Distance': 1600, 'DistanceDisplay': '1600 
METRES', 'Name': 'GEORGE MAIN STAKES', 'NameForm': 'GEO MAIN'}
{'Number': 7, 'NumberDisplay': '07', 'Distance': 1100, 'DistanceDisplay': '1100 
METRES', 'Name': 'THE SHORTS', 'NameForm': 'THE SHORTS'}
{'Number': 8, 'NumberDisplay': '08', 'Distance': 2000, 'DistanceDisplay': '2000 
METRES', 'Name': 'KINGTON TOWN STAKES', 'NameForm': 'KING TOWN'}
{'Number': 9, 'NumberDisplay': '09', 'Distance': 1200, 'DistanceDisplay': '1200 
METRES', 'Name': 'BM 84 HANDICAP', 'NameForm': 'BM84'}

My goal is to select a few elements and create a list of 3 element tuples
like this
[('CLASS 3 HANDICAP', 1, 1000), ('BM 90 HANDICAP', 2, 1600), ('HERITAGE 
STAKES', 3, 1100), ('BILL RITCHIE HANDICAP', 4, 1400), ('TEA ROSE STAKES', 5, 
1400), ('GEORGE MAIN STAKES', 6, 1600), ('THE SHORTS', 7, 1100), ('KINGTON TOWN 
STAKES', 8, 2000), ('BM 84 HANDICAP', 9, 1200)]

I get close creating a list of elements but each attempt I try to create the 
list of tuples fails.

This is my closest code

data = r.json()

raceData = []

for item in data["RaceDay"]['Meetings'][0]['Races']:
raceDetails = item['RacingFormGuide']['Event']['Race']
raceData += 
(raceDetails['Name'],raceDetails['Number'],raceDetails['Distance'])

print(raceDetails)

which returns

['CLASS 3 HANDICAP', 1, 1000, 'BM 90 HANDICAP', 2, 1600, 'HERITAGE STAKES', 3, 
1100, 'BILL RITCHIE HANDICAP', 4, 1400, 'TEA ROSE STAKES', 5, 1400, 'GEORGE 
MAIN STAKES', 6, 1600, 'THE SHORTS', 7, 1100, 'KINGTON TOWN STAKES', 8, 2000, 
'BM 84 HANDICAP', 9, 1200]

How do I get the tuples?

Cheers

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


Re: Why is my class undefined?

2017-08-16 Thread Sayth Renshaw
On Thursday, 17 August 2017 09:03:59 UTC+10, Ian  wrote:
 wrote:
> > Morning
> >
> > I haven't ventured into classes much before. When trying to follow some 
> > examples and create my own classes in a jupyter notebook I receive an error 
> > that the class is undefined.
> >
> > So I created for practise a frog class
> >
> > class frog(object):
> >
> > def __init__(self, ftype, word):
> > self.ftype = ftype
> > self.word = ftype
> >
> > def jump(self):
> > """
> > Make frog jump
> > """
> > return "I am jumping"
> >
> > if __name__ == "__main__":
> > tree_frog = frog("Tree Frog", "Ribbitt")
> > print(frog.ftype)
> > print(frog.word)
> >
> >
> > I receive this error
> >
> > NameError Traceback (most recent call last)
> >  in ()
> > > 1 class frog(object):
> >   2
> >   3 def __init__(self, ftype, word):
> >   4 self.ftype = ftype
> >   5 self.word = ftype
> >
> >  in frog()
> >  12
> >  13 if __name__ == "__main__":
> > ---> 14 tree_frog = frog("Tree Frog", "Ribbitt")
> >  15 print(frog.ftype)
> >  16 print(frog.word)
> >
> > NameError: name 'frog' is not defined
> >
> > what exactly am I doing wrong?
> 
> The if __name__ == "__main__" block is inside the class declaration
> block, so at the point that it runs the class has not been created
> yet. Try removing the indentation to place it after the class block
> instead.

Thank you that had me bugged I just couldn't see it.

Cheers

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


Why is my class undefined?

2017-08-16 Thread Sayth Renshaw
Morning

I haven't ventured into classes much before. When trying to follow some 
examples and create my own classes in a jupyter notebook I receive an error 
that the class is undefined.

So I created for practise a frog class

class frog(object):

def __init__(self, ftype, word):
self.ftype = ftype
self.word = ftype

def jump(self):
"""
Make frog jump
"""
return "I am jumping"

if __name__ == "__main__":
tree_frog = frog("Tree Frog", "Ribbitt")
print(frog.ftype)
print(frog.word)


I receive this error

NameError Traceback (most recent call last)
 in ()
> 1 class frog(object):
  2 
  3 def __init__(self, ftype, word):
  4 self.ftype = ftype
  5 self.word = ftype

 in frog()
 12 
 13 if __name__ == "__main__":
---> 14 tree_frog = frog("Tree Frog", "Ribbitt")
 15 print(frog.ftype)
 16 print(frog.word)

NameError: name 'frog' is not defined

what exactly am I doing wrong?

Cheers

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


Re: Test 0 and false since false is 0

2017-07-08 Thread Sayth Renshaw

> Another option is to test for type(value) == int:
> 
> >>> before = ["a",0,0,"b",None,"c","d",0,1,False,0,1,0,3,[],0,1,9,0,0,
> {},0,0,9]
> >>> wanted = ["a","b",None,"c","d",1,False,1,3,[],1,9,
> {},9,0,0,0,0,0,0,0,0,0,0]
> >>> after = sorted(before, key=lambda x: x == 0 and type(x) == int)
> >>> assert str(after) == str(wanted)
> >>> after
> ['a', 'b', None, 'c', 'd', 1, False, 1, 3, [], 1, 9, {}, 9, 0, 0, 0, 0, 0, 
> 0, 0, 0, 0, 0]
> 
> 
> That way float values will be left alone, too:
> 
> >>> sorted([0.0, 0, False, [], "x"], key=lambda x: x == 0 and type(x) == 
> int)
> [0.0, False, [], 'x', 0]

I have been reading this solution 
> >>> after = sorted(before, key=lambda x: x == 0 and type(x) == int)

it is really good, however I don't understand it enough to reimplement 
something like that myself yet.

Though I can that lambda tests for 0 that is equal to an int why does sorted 
put them to the end?

Cheers

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


Re: Test 0 and false since false is 0

2017-07-06 Thread Sayth Renshaw
On Friday, 7 July 2017 12:46:51 UTC+10, Rick Johnson  wrote:
> On Thursday, July 6, 2017 at 9:29:29 PM UTC-5, Sayth Renshaw wrote:
> > I was trying to solve a problem and cannot determine how to filter 0's but 
> > not false.
> > 
> > Given a list like this
> > ["a",0,0,"b",None,"c","d",0,1,False,0,1,0,3,[],0,1,9,0,0,{},0,0,9]
> > 
> > I want to be able to return this list
> > ["a","b",None,"c","d",1,False,1,3,[],1,9,{},9,0,0,0,0,0,0,0,0,0,0]
> > 
> > However if I filter like this 
> > 
> > def move_zeros(array):
> > l1 = [v for v in array if v != 0]
> > l2 = [v for v in array if v == 0]
> > return l1 + l2
> > 
> > I get this 
> > ['a', 'b', None, 'c', 'd', 1, 1, 3, [], 1, 9, {}, 9, 0, 0, 0, False, 0, 0, 
> > 0, 0, 0, 0, 0] 
> > 
> > I have tried or conditions of v == False etc but then the 0's being false 
> > also aren't moved. How can you check this at once?
> 
> Yep. This is a common pitfall for noobs, as no logic can
> explain to them why integer 0 should bool False, and integer
> 1 should bool True. But what's really going to cook your
> noodle is when you find out that any integer greater than 1
> bools True. Go figure! They'll say it's for consistency
> sake. But i say it's just a foolish consistency.
> 
> You need to learn the subtle difference between `==` and
> `is`.
> 
> ## PYTHON 2.x
> >>> 1 == True
> True
> >>> 1 is True
> False
> >>> 0 == False
> True
> >>> 0 is False
> False

Is there an "is not" method that's not != so I can check is not false.

def move_zeros(array):
l1 = [v for v in array if v is False or v != 0]
l2 = [v for v in array if v is not False or v == 0]
return l1 + l2

Cheers

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


Test 0 and false since false is 0

2017-07-06 Thread Sayth Renshaw
I was trying to solve a problem and cannot determine how to filter 0's but not 
false.

Given a list like this
["a",0,0,"b",None,"c","d",0,1,False,0,1,0,3,[],0,1,9,0,0,{},0,0,9]

I want to be able to return this list
["a","b",None,"c","d",1,False,1,3,[],1,9,{},9,0,0,0,0,0,0,0,0,0,0]

However if I filter like this 

def move_zeros(array):
l1 = [v for v in array if v != 0]
l2 = [v for v in array if v == 0]
return l1 + l2

I get this 
['a', 'b', None, 'c', 'd', 1, 1, 3, [], 1, 9, {}, 9, 0, 0, 0, False, 0, 0, 0, 
0, 0, 0, 0] 

I have tried or conditions of v == False etc but then the 0's being false also 
aren't moved. How can you check this at once?


Cheers

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


Re: Generator - Provide parameters to url - requests

2017-07-05 Thread Sayth Renshaw
Thanks. 

I left "base" out as i was trying to remove as much uneeded code from example 
as possible. I had defined it as

base = datetime.datetime(2017,1,1)

Reading your code this sounds to simple :-).

def dates(first, numdays): 
# generate datetime objects for extra clarity 
# note there are no implicit arguments like `base` in your code 
for _ in range(numdays): 
yield first 
first += ONE_DAY 

Thanks

Sayth

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


Generator - Provide parameters to url - requests

2017-07-05 Thread Sayth Renshaw
Hi

I am struggling to figure out how I can create a generator to provide values to 
my url. My url needs to insert the year month and day in the url not as params 
to the url.


import json
import requests
import datetime

# using this I can create a list of dates for the first 210 days of this year.

base = datetime.datetime(2017,1,1)

def datesRange(max, min):
date_list = (base - datetime.timedelta(days=x) for x in range(max,min))
DAY = date_list.day
MONTH = date_list.month
YEAR = date_list.year
yield DAY, MONTH, YEAR

dateValues = datesRange(-210,0)

def create_url(day, month, year):
   
https://api.tatts.com/sales/vmax/web/data/racing/{0}/{1}/{2}/sr/full".format(YEAR,MONTH,DAY)
return url

Then I want to insert them in this url one at a time from the generator

try:
   r = requests.get(INSERT_URL_HERE)
   if r.status_code == requests.codes.ok then:
  # open a file for writing using url paramters
  with open(SR + DAY + MONTH + YEAR + '.json', 'w') as f:
  # Do stuff from here not relevant to question.

I have just gotten lost.

Is there an easier way to go about this?

Cheers

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


Re: Create a list of dates for same day of week in a year

2017-06-28 Thread Sayth Renshaw

> > Thoughts or examples?
> > 
> dateutil.rrule is what you may use e.g.
> 
> 
> In [38]: from dateutil import rrule 
> 
> In [39]: from datetime import date  
> 
> In [40]: end = date(2017, 12, 31)   
> 
> In [41]: rr = rrule.rrule(rrule.WEEKLY, byweekday=[0, 2], until=end)
> 
> In [42]: days = list(rr)
> 
> In [43]: len(days)  
> Out[43]: 53 
> 
> In [44]: days[:5], days[-5:]
> Out[44]:
> ([datetime.datetime(2017, 6, 28, 23, 58, 11),   
>   datetime.datetime(2017, 7, 3, 23, 58, 11),
>   datetime.datetime(2017, 7, 5, 23, 58, 11),
>   datetime.datetime(2017, 7, 10, 23, 58, 11),   
>   datetime.datetime(2017, 7, 12, 23, 58, 11)],  
>  [datetime.datetime(2017, 12, 13, 23, 58, 11),  
>   datetime.datetime(2017, 12, 18, 23, 58, 11),  
>   datetime.datetime(2017, 12, 20, 23, 58, 11),  
>   datetime.datetime(2017, 12, 25, 23, 58, 11),  
>   datetime.datetime(2017, 12, 27, 23, 58, 11)]) 
> 
> In [45]:

Thanks. I am just researching now the format that has come out. unclear what 58 
represents.

Cheers

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


Re: Create a list of dates for same day of week in a year

2017-06-27 Thread Sayth Renshaw

> > Is there an obvious method I am missing in creating a list of dates? I want 
> > to get a list of each Saturday and each Wednesday for the year 2017.
> >
> > It seems and maybe this is where I am wrong but doesn't the datetime 
> > library already know the dates if yes is there an easy way to query it?
> >
> 
> Sorta-kinda.
> 
> >>> import datetime
> >>> today = datetime.date.today()
> >>> monday = today - datetime.timedelta(days=today.weekday())
> >>> wednesday = monday + datetime.timedelta(days=2)
> >>> saturday = monday + datetime.timedelta(days=5)
> >>> week = datetime.timedelta(days=7)
> >>> next_wed = wednesday + week
> 
> You can mess around with that. If you want to find the beginning of
> the year, you could start by using datetime.date(2017, 1, 1) instead
> of today(), and then locate the previous Monday the same way.
> 
> So yes, all the information is available, but no, there's no easy way
> to say "give me all the Saturdays in 2017". You'd have to iterate.
> 
> ChrisA

Thanks ChrisA could using the calendar and a for loop work better?

As below test a date to see if the value = 5 so loop months 1-12 and then days 
1-31 and just throw an error when the date doesn't exist 30th of Feb etc
In [19]: import calendar


In [23]: calendar.weekday(2017,6,24)
Out[23]: 5

Or I have set the Saturday to be the firstday of the week 
https://docs.python.org/2/library/calendar.html is there a way to loop for all 
firstdayofweeks in year and return dates out that way?

Not sure how to get all dates in the year as an object for iteration though. 
The calendar iter items end at months 
https://docs.python.org/2/library/calendar.html#calendar.Calendar.itermonthdays

In [20]: calendar.setfirstweekday(calendar.SATURDAY)

In [21]: calendar.firstweekday()
Out[21]: 5

Cheers

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


Create a list of dates for same day of week in a year

2017-06-27 Thread Sayth Renshaw
Afternoon

Is there an obvious method I am missing in creating a list of dates? I want to 
get a list of each Saturday and each Wednesday for the year 2017.

It seems and maybe this is where I am wrong but doesn't the datetime library 
already know the dates if yes is there an easy way to query it?

Also checked out Python Arrow http://arrow.readthedocs.io/en/latest/ as it 
expands on the python standard library but I couldn't find an obvious example 
of this.

Thoughts or examples?

Cheers

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


Re: Better way to do this dict comprehesion

2017-03-08 Thread Sayth Renshaw
Peter I really like this

The complete code: 

>>> from collections import Counter 
>>> def find_it(seq): 
... [result] = [k for k, v in Counter(seq).items() if v % 3 == 0] 
... return result 
... 
>>> test_seq = [20,1,-1,2,-2,3,3,5,5,1,2,4,20,4,-1,-2,5] 
>>> find_it(test_seq) 

But what may be the smallest thing in this i had no idea I could do [result] = 
blah and get a generator on the return variable that seems insane.

Cheers

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


Re: Better way to do this dict comprehesion

2017-03-07 Thread Sayth Renshaw

> > But the given problem states there will always only be one number appearing 
> > an odd number of times given that is there a neater way to get the answer?
> 
> Take a step back for a moment. Are you trying to find something that
> appears an odd number of times, or a number of times that counts by
> three? First figure that out, THEN see if there's a better way to do
> what you're doing.

A number that appears an odd number of times.

> 
> To find an unpaired number in linear time with minimal space, try
> stepping through the list and either adding to a set or removing from
> it. At the end, your set should contain exactly one element. I'll let
> you write the actual code :)
> 
> ChrisA

ChrisA the way it sounds through the list is like filter with map and a lambda. 
http://www.python-course.eu/lambda.php Haven't had a good go yet... will see 
how it goes.

Thanks

Sayth

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


Better way to do this dict comprehesion

2017-03-07 Thread Sayth Renshaw
Hi

I have got this dictionary comprehension and it works but how can I do it 
better?

from collections import Counter

def find_it(seq):
 counts = dict(Counter(seq))
 a = [(k, v) for k,v in counts.items() if v % 3 == 0]
 return a[0][0]

test_seq = [20,1,-1,2,-2,3,3,5,5,1,2,4,20,4,-1,-2,5]

so this returns 5 which is great and the point of the problem I was doing.

Can also do it like this
def find_it(seq):
 counts = dict(Counter(seq))
 a = [(k) for k,v in counts.items() if v % 3 == 0]
 return a[0]

But the given problem states there will always only be one number appearing an 
odd number of times given that is there a neater way to get the answer?


Thanks

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


Re: How to flatten only one sub list of list of lists

2017-03-01 Thread Sayth Renshaw

> Replace the slice row[index:index+1] with row[index], either by building a 
> new list or in place:
> 
> >>> def show(data):
> ...for item in data: print(item)
> ... 
> >>> def flatten_one(rows, index):
> ... return [r[:index] + r[index] + r[index+1:] for r in rows]
> ... 

> >>> def flatten_inplace(rows, index):
> ... for row in rows:
> ... row[index:index+1] = row[index]
> ... 
> >>> flatten_inplace(data, 5)
> >>> show(data)
> ['46295', 'Montauk', '3', '60', '85', '19', '5', '1', '0 $277790.00']
> ['46295', 'Dark Eyes', '5', '59', '83', '6', '4', '1', '0 $105625.00']
> ['46295', 'Machinegun Jubs', '6', '53', '77', '6', '2', '1', '1 $71685.00']
> ['46295', 'Zara Bay', '1', '53', '77', '12', '2', '3', '3 $112645.00']

I went for the one I can understand which was inplace

def flatten_inplace(rows, index):
for row in rows:
row[index:index + 1] = row[index]

return rows

See now if I can make it more adaptable to use it in some other situations, 
quite useful.

Thanks

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


How to flatten only one sub list of list of lists

2017-02-28 Thread Sayth Renshaw
How can I flatten just a specific sublist of each list in a list of lists?

So if I had this data


[   ['46295', 'Montauk', '3', '60', '85', ['19', '5', '1', '0 $277790.00']],
['46295', 'Dark Eyes', '5', '59', '83', ['6', '4', '1', '0 $105625.00']],
['46295', 'Machinegun Jubs', '6', '53', '77', ['6', '2', '1', '1 
$71685.00']],
['46295', 'Zara Bay', '1', '53', '77', ['12', '2', '3', '3 $112645.00']]]


How can I make it be


[   ['46295', 'Montauk', '3', '60', '85', '19', '5', '1', '0 $277790.00'],
['46295', 'Dark Eyes', '5', '59', '83', '6', '4', '1', '0 $105625.00'],
['46295', 'Machinegun Jubs', '6', '53', '77', '6', '2', '1', '1 $71685.00'],
['46295', 'Zara Bay', '1', '53', '77', '12', '2', '3', '3 $112645.00']]

Been looking around but most solutions just entirely flatten everything.
This was popular on SO but yeah it flattens everything  I want to be more 
selective

def flatten(lst):
for elem in lst:
if type(elem) in (tuple, list):
for i in flatten(elem):
yield i
else:
yield elem

What I am thinking is that if for each list the sublist should be at index 1, so

[0][1]
[1][1]
[2][1]

for item in list:
item[1] - somehow flatten.

Thoughts?

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


Is there a good process or library for validating changes to XML format

2017-01-06 Thread Sayth Renshaw
Afternoon

Is there a good library or way I could use to check that the author of the XML 
doc I am using doesn't make small changes to structure over releases?

Not fully over this with XML but thought that XSD may be what I need, if I 
search "python XSD" I get a main result for PyXB and generateDS 
(https://pythonhosted.org/generateDS/).

Both seem to be libraries for generating bindings to structures for parsing so 
maybe I am searching the wrong thing. What is the right thing to search?

Cheers

Sayth

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


Re: Screwing Up looping in Generator

2017-01-06 Thread Sayth Renshaw
For completeness I was close this is the working code.

def get_list_of_names(generator_arg):
name_set = set()
for name in generator_arg:
base = os.path.basename(name.name)
filename = os.path.splitext(base)[0]
name_set.add(filename)
return name_set


def data_attr(roots):
"""Get the root object and iter items."""
for name in names:
directory = "output"
write_name = name + ".csv"
with open(os.path.join(directory, write_name), 'w', newline='') as
csvf:
race_writer = csv.writer(csvf, delimiter=','
)


thanks for your time and assistance. It's much appreciated

Sayth

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


Re: Screwing Up looping in Generator

2017-01-06 Thread Sayth Renshaw
Untested as i wrote this in notepad at work but, if i first use the generator 
to create a set of filenames and then iterate it then call the generator anew 
to process file may work?

Good idea or better available?

def get_list_of_names(generator_arg):
name_set = set()
for name in generator_arg:
base = os.path.basename(name.name)
filename = os.path.splitext(base)[0]
name_set.add(filename)
return name_set



 for file_name in name_set:
 directory = "output"
 with open(os.path.join(directory, filename, 'w', newline='') as csvf:
for file in rootobs:
  # create and write csv

Sayth

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


Re: Screwing Up looping in Generator

2017-01-06 Thread Sayth Renshaw
So can I call the generator twice and receive the same file twice in 2 for 
loops?

Once to get the files name and the second to process?

 for file in rootobs:
base = os.path.basename(file.name)
write_to = os.path.join("output", os.path.splitext(base)[0] + ".csv")
with open(write_to, 'w', newline='') as csvf:
for file in rootobs:
  # create and write csv

Cheers

Sayth

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


Re: Screwing Up looping in Generator

2017-01-06 Thread Sayth Renshaw
On Wednesday, 4 January 2017 12:36:10 UTC+11, Sayth Renshaw  wrote:
> So can I call the generator twice and receive the same file twice in 2 for
loops?
>
> Once to get the files name and the second to process?
>
>  for file in rootobs:
> base = os.path.basename(file.name)
> write_to = os.path.join("output", os.path.splitext(base)[0] + ".csv")
> with open(write_to, 'w', newline='') as csvf:
> for file in rootobs:
>       # create and write csv
>
> Cheers
>
> Sayth

I just need it to write after each file however the

with open(#file) as csvf:

Keeps it all open until every file processed in an output file with the name of 
the first file in the generator.

Sayth

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


Re: Is there a good process or library for validating changes to XML format?

2017-01-05 Thread Sayth Renshaw
It definitely has more features than i knew http://xmlsoft.org/xmllint.html

Essentially thigh it appears to be aimed at checking validity and compliance of 
xml.
I why to check the structure of 1 xml file against the previous known structure 
to ensure there are no changes.

Cheers

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


Is there a good process or library for validating changes to XML format?

2017-01-04 Thread Sayth Renshaw
Afternoon

Is there a good library or way I could use to check that the author of the XML 
doc I am using doesn't make small changes to structure over releases?

Not fully over this with XML but thought that XSD may be what I need, if I 
search "python XSD" I get a main result for PyXB and generateDS 
(https://pythonhosted.org/generateDS/). 

Both seem to be libraries for generating bindings to structures for parsing so 
maybe I am searching the wrong thing. What is the right thing to search?

Cheers

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


Re: Screwing Up looping in Generator

2017-01-04 Thread Sayth Renshaw
For completeness I was close this is the working code.

def get_list_of_names(generator_arg):
name_set = set()
for name in generator_arg:
base = os.path.basename(name.name)
filename = os.path.splitext(base)[0]
name_set.add(filename)
return name_set


def data_attr(roots):
"""Get the root object and iter items."""
for name in names:
directory = "output"
write_name = name + ".csv"
with open(os.path.join(directory, write_name), 'w', newline='') as csvf:
race_writer = csv.writer(csvf, delimiter=','
)


thanks for your time and assistance. It's much appreciated

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


Re: Screwing Up looping in Generator

2017-01-03 Thread Sayth Renshaw
Untested as i wrote this in notepad at work but, if i first use the generator 
to create a set of filenames and then iterate it then call the generator anew 
to process file may work?

Good idea or better available?

def get_list_of_names(generator_arg):
name_set = set()
for name in generator_arg:
base = os.path.basename(name.name)
filename = os.path.splitext(base)[0]
name_set.add(filename)
return name_set



 for file_name in name_set: 
 directory = "output"
 with open(os.path.join(directory, filename, 'w', newline='') as csvf: 
for file in rootobs: 
  # create and write csv 

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


Re: Screwing Up looping in Generator

2017-01-03 Thread Sayth Renshaw
On Wednesday, 4 January 2017 12:36:10 UTC+11, Sayth Renshaw  wrote:
> So can I call the generator twice and receive the same file twice in 2 for 
> loops?
> 
> Once to get the files name and the second to process?
> 
>  for file in rootobs: 
> base = os.path.basename(file.name) 
> write_to = os.path.join("output", os.path.splitext(base)[0] + ".csv") 
> with open(write_to, 'w', newline='') as csvf: 
> for file in rootobs:
>   # create and write csv
> 
> Cheers
> 
> Sayth

I just need it to write after each file however the 

with open(#file) as csvf:

Keeps it all open until every file processed in an output file with the name of 
the first file in the generator.

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


Re: Screwing Up looping in Generator

2017-01-03 Thread Sayth Renshaw
So can I call the generator twice and receive the same file twice in 2 for 
loops?

Once to get the files name and the second to process?

 for file in rootobs: 
base = os.path.basename(file.name) 
write_to = os.path.join("output", os.path.splitext(base)[0] + ".csv") 
with open(write_to, 'w', newline='') as csvf: 
for file in rootobs:
  # create and write csv

Cheers

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


  1   2   3   >