Re: [Tutor] Variable change within library depending on user input.

2019-03-04 Thread Peter Otten
Harry Oneill wrote:

> Hey there everybody hope your doing great.
> 
> I was here a few months ago and got pointed in the right direction very
> kindly by one of the tutors. Im a little stuck again now and have been
> researching for a while and can't come up with a solution to my problem.
> 
> The below program is designed to take two user inputs, item purchase
> location and sale location and then to compare some dictionaries of those
> locations and calculate the most profitable item to buy.
> 
> Initially i designed this to work with just two locations and am now
> struggling to add a third.

Do you still have the version that works with two locations?
Then start from that with the following steps:

(1) Extract the code that uses a buying and a selling, and finds the most 
profitable good into a function

def find_most_profitable(buying_dict, selling_dict):
... # code adapted from the version that works with two locations

(2) Write the main script that picks a buying dict and a selling dict using 
user input. The simplest approach is to put the buying dicts into another 
dict:

buying_dicts = {"olisar": buying_olisar, "levski": buying_levski, "156": 
buying_arc_corp_mining_area_157}
selling_dicts = ...

def pick_dict(choices, prompt):
print(prompt)
while True:
print("Pick one of", ", ".join(sorted(choices)))
choice = input("> ")
if choice in choices:
return choices[choice]

buying =  pick_dict(
buying_dicts,
"Were are you planning to buy your goods?"
)
selling = pick_dict(
selling_dicts,
"Where are you planning to sell your goods?"
)

find_most_profitable(buying, selling)

As a bonus the pick_dict() function checks the validity of your choice and 
lets you repeat your input if you made a spelling error.

> However they produce this error that i am unsure how to resolve.
> 
> Traceback (most recent call last):
>   File "/home/floppypoppy/PycharmProjects/Star Citizen Trading
>   Program/Star Citizen trading NEW.py", line 33, in 
> tradable_items = common(user_purchase_location_1,
> user_sale_destination_1)
>   File "/home/floppypoppy/PycharmProjects/Star Citizen Trading
>   Program/Star Citizen trading NEW.py", line 32, in common
> return set(curr.keys()).intersection(set(other.keys()))
> AttributeError: 'str' object has no attribute 'keys'

Now why did you see the traceback?

>  Sorting the dict into ascending value instead of alphabetical
> sort_buying_olisar = sorted(buying_olisar, key=lambda tup: tup[1])


Sorting a dict implies iteration, and iterating over a dict produces the 
keys:

>>> d = {"az": 2, "by": 1, "cx": 3}
>>> list(d)
['cx', 'by', 'az']

Therefore

>>> sorted(d, key=lambda k: k[1])
['cx', 'by', 'az']

sorts the keys by the second character.
To sort the (key, value) pairs by value you need to be explicit:

>>> sorted(d.items(), key=lambda kv: kv[1])
[('by', 1), ('az', 2), ('cx', 3)]

If you are using Python 3.7 (and inofficially 3.6 I think) you can convert 
this back into a dict with

items = sorted(d.items(), key=lambda kv: kv[1])
ordered = dict(items)

In older Pythons you may need

ordered = collections.OrderedDict(items)

instead.



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


[Tutor] Python Regular Expressions (Re.sub) Function

2019-03-04 Thread Edward Kanja
Hi there,
Hope this finds you well, I'm working on a simple project to analyse data
using
regular expressions. I have successfully worked on a regular expression
pattern
that extracts data from my txt file. Unfortunately after i extract the data
my output has too much of square brackets and by so doing the output cant
be well exported in a csv file. I have worked on an idea of Re.sub function
where i'm trying to replace all square brackets in my output with an empty
string.
Below are images of what i have been working on.

Explanations of the attachments and images named as follows:
*1. mydata in txtfile. *This images shows how my data looks in the txt file
although
i have also attached the real txt file with the actual data below.
*2. code for the output. *This image shows the regular expression that is
well extracting the data from my txt file.
3. *my output. *This image shows the out after i ran my code . it shows the
square brackets that are in my output and ones i need to do away with.
4.*tryone code. *This image shows my first try using re.sub to eliminate
the square brackets.
*5. tryone output.*This images shows the output i had after running the
code in the image named tryone code.
*6. try two code:*This image shows my second try using re.sub to eliminate
the square brackets.
*7.trytwo output.*This images shows the output i had after running the code
in the image named trytwo code.
*8.try four code.*This image shows my fourth try using re.sub to eliminate
the square brackets.
*9.try four output.* This images shows the output i had after running the
code in the image named tryfour code.
*10.try five code.*This image shows my number five try using re.sub to
eliminate the square brackets
*11.*  *try five output.*This images shows the output i had after running
the code in the image named tryfive code.
*12.line five name code regex.*This image shows the full code for the name
regular expression with the re.sub function for the code
in the image named try five code.
*13.try six code.*This image shows my sixth try using re.sub to eliminate
the square brackets
*14. try six output.*This images shows the output i had after running the
code in the image named tryfsix code
*15.try six full name code.*This image shows the full code for the name
regular expression with the re.sub function for the code
in the image named try six code.
*16. unon.TXT. *This is the txt file that has the data i am extracting.

Kindly assist and kindly copy my work email ( edward.mwa...@un.org ) when
replying .
Thanks alot.

Regards,
Kanja Edward.
P.O.BOX 1203-00300,
NAIROBI.
*+254720724793*
www.linkedin.com/in/edward-kanja-bba16a106

--
|Rawzeea NLKPP | VE11-Nairobi   
 | 20002254-MADIZ| 00   | 00   
|Regular Scheme B | 15-JAN-2019 To 31-DEC-2019 | No   |
||
| 3648 | 18-FEB-2020 | Yes | 03 
 |  .00 USD  | 0.00 USD  | Leased Commercially  
 |   00.00 KES| No   |
||
| R-9-01   | 00.00%  |0023.28 KES  |000.18 USD  
 |   |  1.0  |  0.00|  0.00| No 
 | 10-JAN-2019 To 31-DEC-2019|
-|
   | Valid From: 17-Jan-2017
 | Warwass Center   
 |
   | Warwass Center 
 | UK-8 
 |
   |
 | UK Avenue
 |
   | Nairobi---Kenya
 |  
 

Re: [Tutor] Python Regular Expressions (Re.sub) Function

2019-03-04 Thread Steven D'Aprano
Hi Edward, and welcome.

Please remember that we're volunteers, doing this for free. Unless your 
problem is really interesting, you're not likely to get people 
volunteering to spend a long time slogging through multiple attachments, 
screenshots, at least five seperate attempts, etc.

By the way, most of your attachments have been deleted by the mailing 
list. Only things which it considers text files will get through.

Please try to simplify the question you are asking, and keep the code 
you show as short and simple as practical. You might find it helpful to 
read this:

http://www.sscce.org/

In the meantime, this might help you:

py> import re
py> text = "abcd[][]...[x]yz"
py> re.sub(r'\[|\]', '@', text)
'abcd...@x@yz'
py> re.sub(r'\[|\]', '', text)
'abcd...xyz'






On Mon, Mar 04, 2019 at 11:04:15AM +0300, Edward Kanja wrote:
> Hi there,
> Hope this finds you well, I'm working on a simple project to analyse data
> using
> regular expressions. I have successfully worked on a regular expression
> pattern
> that extracts data from my txt file. Unfortunately after i extract the data
> my output has too much of square brackets and by so doing the output cant
> be well exported in a csv file. I have worked on an idea of Re.sub function
> where i'm trying to replace all square brackets in my output with an empty
> string.
> Below are images of what i have been working on.
> 
> Explanations of the attachments and images named as follows:
> *1. mydata in txtfile. *This images shows how my data looks in the txt file
> although
> i have also attached the real txt file with the actual data below.
> *2. code for the output. *This image shows the regular expression that is
> well extracting the data from my txt file.
> 3. *my output. *This image shows the out after i ran my code . it shows the
> square brackets that are in my output and ones i need to do away with.
> 4.*tryone code. *This image shows my first try using re.sub to eliminate
> the square brackets.
> *5. tryone output.*This images shows the output i had after running the
> code in the image named tryone code.
> *6. try two code:*This image shows my second try using re.sub to eliminate
> the square brackets.
> *7.trytwo output.*This images shows the output i had after running the code
> in the image named trytwo code.
> *8.try four code.*This image shows my fourth try using re.sub to eliminate
> the square brackets.
> *9.try four output.* This images shows the output i had after running the
> code in the image named tryfour code.
> *10.try five code.*This image shows my number five try using re.sub to
> eliminate the square brackets
> *11.*  *try five output.*This images shows the output i had after running
> the code in the image named tryfive code.
> *12.line five name code regex.*This image shows the full code for the name
> regular expression with the re.sub function for the code
> in the image named try five code.
> *13.try six code.*This image shows my sixth try using re.sub to eliminate
> the square brackets
> *14. try six output.*This images shows the output i had after running the
> code in the image named tryfsix code
> *15.try six full name code.*This image shows the full code for the name
> regular expression with the re.sub function for the code
> in the image named try six code.
> *16. unon.TXT. *This is the txt file that has the data i am extracting.
> 
> Kindly assist and kindly copy my work email ( edward.mwa...@un.org ) when
> replying .
> Thanks alot.
> 
> Regards,
> Kanja Edward.
> P.O.BOX 1203-00300,
> NAIROBI.
> *+254720724793*
> www.linkedin.com/in/edward-kanja-bba16a106
> 

> --
> |Rawzeea NLKPP | VE11-Nairobi 
>| 20002254-MADIZ| 00   | 00   
> |Regular Scheme B | 15-JAN-2019 To 31-DEC-2019 | No   |
> ||
> | 3648 | 18-FEB-2020 | Yes | 03   
>|  .00 USD  | 0.00 USD  | Leased Commercially  
>  |   00.00 KES| No   |
> ||
> | R-9-01   | 00.00%  |0023.28 KES  |000.18 
> USD   |   |  1.0  |  0.00|  0.00  
>   | No  | 10-JAN-2019 To 31-DEC-2019|
> -

Re: [Tutor] Python Regular Expressions (Re.sub) Function

2019-03-04 Thread Alan Gauld via Tutor
On 04/03/2019 08:04, Edward Kanja wrote:
> ... Unfortunately after i extract the data
> my output has too much of square brackets and by so doing the output cant
> be well exported in a csv file. 

I can't see the data so can't be definitive but the number of square
brackets shouldn't affect a CSV file (except that it will be messy to
read). Are you using the csv module to produce the csv file?
It is much safer to do that than to try to write a csv file yourself.

> I have worked on an idea of Re.sub function
> where i'm trying to replace all square brackets in my output with an empty
> string.

See Steve's reply.

> Below are images of what i have been working on.

Images don't work on this (and many other) mailing list.
The server sees them as potential security threats and
throws them away. Please put sample code directly in
the mail message or, if it is very long ( >100 lines?)
paste it on a pastebin type web site and include a link.

> 4.*try one code. 
> *6. try two code:
> *8.try four code.
> *10.try five code.
> *13.try six code.
Multiple versions of the code just results in confusion
as different people respond to different versions.
Pick one version that's closest to what you want and
use that as the basis of discussion. Ideally remove all
code that's not directly relevant to the problem,
eg the code that loads your data in the first place.
Show a small sample of input data and the output data.
Show us any error messages in full.

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


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


[Tutor] My problem in simple terms

2019-03-04 Thread Edward Kanja
Hi there ,
Earlier i had sent an email on how to use re.sub function to eliminate
square brackets. I have simplified the statements. Attached txt file named
unon.Txt has the data im extracting from. The file named code.txt has the
codes I'm using to extract the data.The regular expression works fine but
my output has too many square brackets. How do i do away with them thanks.



Regards,
Kanja Edward.
P.O.BOX 1203-00300,
NAIROBI.
*+254720724793*
www.linkedin.com/in/edward-kanja-bba16a106 
--
|Rawzeea NLKPP | VE11-Nairobi   
 | 20002254-MADIZ| 00   | 00   
|Regular Scheme B | 15-JAN-2019 To 31-DEC-2019 | No   |
||
| 3648 | 18-FEB-2020 | Yes | 03 
 |  .00 USD  | 0.00 USD  | Leased Commercially  
 |   00.00 KES| No   |
||
| R-9-01   | 00.00%  |0023.28 KES  |000.18 USD  
 |   |  1.0  |  0.00|  0.00| No 
 | 10-JAN-2019 To 31-DEC-2019|
-|
   | Valid From: 17-Jan-2017
 | Warwass Center   
 |
   | Warwass Center 
 | UK-8 
 |
   |
 | UK Avenue
 |
   | Nairobi---Kenya
 |  
 |
   |
 | Nairobi--00100-Kenya 
 |
   |
 | 0207690606   
 |
   |
 | hous...@weekly.com   
  |
   
---
--
|Pattly MUNIIZ | TX00-Nairobi   
 | 2006-KZAU | 00   | 00   
|Regular Scheme B | 05-JAN-2010 To 01-JAN-2019 | No   |
||
| 1007 | 07-DEC-2019 | No  | 00 
 |  .00 USD  | 0.00 USD  | Leased Commercially  
 |   00.00 KES| No   |
||
| K-3-10   | 00.00%  |.25 KES  |.60 USD 
 |   |  1.0  |  0.00|  0.00| No 
 

Re: [Tutor] My problem in simple terms

2019-03-04 Thread Alan Gauld via Tutor
On 04/03/2019 11:44, Edward Kanja wrote:
> Hi there ,
> Earlier i had sent an email on how to use re.sub function to eliminate
> square brackets. I have simplified the statements. Attached txt file named
> unon.Txt has the data im extracting from. 

Thankyou, that's much better. Although the code is now short enough to
just paste into the mail:

###
import pandas as pd
from pandas import DataFrame #creating my dataframes manually.
import re
#import textfile

with open ('unon.txt') as csvfile:
   mydata=pd.read_csv('unon.txt')
   for line in csvfile:
  index_no=re.findall(r'(\|\s\d{5,8}\s)',line)
  names=re.findall(r'(\|[A-Za-z]\w*\s\w*\s\w*\s\w*\s)',line)
  #Address=re.findall(r'\|\s([A-Z0-9-,/]\w*\s\w*\s)',line)
  duty_station=re.findall(r'\|\s[A-Z]*\d{2}\-\w\w\w\w\w\w\w\s',line)
  print((index_no),(names),(duty_station))


You don't need the pandas line since you don't do anything
with mydata.

Also your data is not really a csv file so I'm not sure how Pandas
will cope with it.

> codes I'm using to extract the data.The regular expression works fine but
> my output has too many square brackets. How do i do away with them thanks.

Can you show us the output? I can't see how you can have any square
brackets since your data has none. I'm guessing you maybe mean the
vertical bar symbols? I suspect the best way to remove them is to
improve the regexes used for extraction. There are web sites that allow
you to paste sample data and then create different regex and see the
output, that may be a useful way forward.

Here is one such:

https://regex101.com/

Assuming that you have multiple input records like the one shown I'd
suggest you put the code to read one record into a function and then
call that repeatedly until the file is processed. You can also create
a function to write one record at a time to a file(using whatever
format you prefer(csv, json, yaml etc). I suspect csv may not be
ideal for this data, but you may want to process it elsewhere
that requires csv, I don;t know. The functions will probably want
to use the file as an iterable since it needs to process multiple
lines per record.

Finally, the parens in the last print line are not needed,
just use:

print(index_no, names, duty_station)

Although I assume this is just debug information so not
really too important.

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


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


Re: [Tutor] My problem in simple terms

2019-03-04 Thread Peter Otten
Edward Kanja wrote:

> Hi there ,
> Earlier i had sent an email on how to use re.sub function to eliminate
> square brackets. I have simplified the statements. Attached txt file named
> unon.Txt has the data im extracting from. The file named code.txt has the
> codes I'm using to extract the data.The regular expression works fine but
> my output has too many square brackets. How do i do away with them thanks.

The square brackets appear because re.findall() returns a list. If you know 
that there is only one match or if you are only interested in the first 
match you can extract it with

first = re.findall(...)[1]

This will of course fail if there is no match at all, so you have to check 
the length first. You can also use the length check to skip the lines with 
no match at all, i. e. the line appearing as

[] [] []

in your script's output.

Now looking at your data -- at least from the sample it seems to be rather 
uniform. There are records separated by "---..." and fields separated by 
"|". I'd forego regular expressions for that:

$ cat code.py
from itertools import groupby

def is_record_sep(line):
return not line.rstrip().strip("-")

with open("unon.txt") as instream:
for sep, group in groupby(instream, key=is_record_sep):
if not sep:
record = [
[field.strip() for field in line.split("|")]
for line in group if line.strip().strip("|-")
]
# select field by their position in the record
names = record[0][1]
station = record[0][2]
index = record[1][1]
print(index, names, station, sep=", ")
$ python3 code.py 
3648, Rawzeea NLKPP, VE11-Nairobi
1007, Pattly MUNIIZ, TX00-Nairobi


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


[Tutor] how to get the weeks of a month

2019-03-04 Thread john fabiani

Hi everyone,

I'm not exactly a newbie but I can't seem to solve this problem.

I need to print out the weeks of the month - given any month and any year.

For example this month would have:

3/1/2019 - 3/3/2019   # notice that this a short week
3/4/2019 - 3/10/2019
3/11/2019 - 3/17/2019
3/18/2019 - 3/24/2019
3/25/2019 - 3/31/2019  # also this can be a short week as in April 2019 
last week would be 4/29/2019 - 4/30-2019


I have tried using isocalendar, dateutil, and just plain datetime.

I get close but no real solution. Google wasn't much help either.  So I 
thought I'd ask here - you have been helpful in the past.  And no at my 
age I am not attending school - so this not my homework.


Johnf

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


Re: [Tutor] how to get the weeks of a month

2019-03-04 Thread Mark Lawrence

On 04/03/2019 18:54, john fabiani wrote:

Hi everyone,

I'm not exactly a newbie but I can't seem to solve this problem.

I need to print out the weeks of the month - given any month and any year.

For example this month would have:

3/1/2019 - 3/3/2019   # notice that this a short week
3/4/2019 - 3/10/2019
3/11/2019 - 3/17/2019
3/18/2019 - 3/24/2019
3/25/2019 - 3/31/2019  # also this can be a short week as in April 2019 
last week would be 4/29/2019 - 4/30-2019


I have tried using isocalendar, dateutil, and just plain datetime.

I get close but no real solution. Google wasn't much help either.  So I 
thought I'd ask here - you have been helpful in the past.  And no at my 
age I am not attending school - so this not my homework.


Johnf



Have you tried any of monthdatescalendar, monthdays2calendar or 
monthdayscalendar from https://docs.python.org/3/library/calendar.html?


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

Mark Lawrence

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


Re: [Tutor] how to get the weeks of a month

2019-03-04 Thread Alan Gauld via Tutor
On 04/03/2019 18:54, john fabiani wrote:

> I need to print out the weeks of the month - given any month and any year.
> 

I'm not totally clear how you define a week.

In your example the first week starts on Friday
and ends on Sunday.

Subsequent weeks start on Monday and run to Sunday.

So, is a week essentially a variable number of days
prior to a Sunday?

What do you do if a month ends on a Thursday,
say? How is the last week printed?
EDIT: OK I see the comment at the end now.

> For example this month would have:
> 
> 3/1/2019 - 3/3/2019   # notice that this a short week
> 3/4/2019 - 3/10/2019
> 3/11/2019 - 3/17/2019
> 3/18/2019 - 3/24/2019
> 3/25/2019 - 3/31/2019  # also this can be a short week as in April 2019 
> last week would be 4/29/2019 - 4/30-2019

So what I think you want is to

start with the first day and print each day up to Sunday.
Newline
print the current date up to sunday
newline
repeat until you run out of days in the month.

datetime has functions for extracting the date,
the day and the month It also has functions for
formatting the date string.

Or you could use calendar to matrix
representation of the month.

>>> import calendar as cal
>>> cal.monthcalendar(2019,3)
[[0, 0, 0, 0, 1, 2, 3], [4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16,
17], [18, 19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30, 31]]
>>>

That looks close to what you want?


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


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


Re: [Tutor] how to get the weeks of a month

2019-03-04 Thread David Rock

> On Mar 4, 2019, at 13:19, Alan Gauld via Tutor  wrote:
> 
> On 04/03/2019 18:54, john fabiani wrote:
> 
>> I need to print out the weeks of the month - given any month and any year.
> 
> I'm not totally clear how you define a week.
> 
> EDIT: OK I see the comment at the end now.
> 
>> For example this month would have:
>> 
>> 3/1/2019 - 3/3/2019   # notice that this a short week
>> 3/4/2019 - 3/10/2019
>> 3/11/2019 - 3/17/2019
>> 3/18/2019 - 3/24/2019
>> 3/25/2019 - 3/31/2019  # also this can be a short week as in April 2019 
>> last week would be 4/29/2019 - 4/30-2019

What I think he’s shooting for is something similar to cal output

$ cal
 March 2019
Su Mo Tu We Th Fr Sa
1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31


> So what I think you want is to
> 
> start with the first day and print each day up to Sunday.
> Newline
> print the current date up to sunday
> newline
> repeat until you run out of days in the month.
> 
> 
 import calendar as cal
 cal.monthcalendar(2019,3)
> [[0, 0, 0, 0, 1, 2, 3], [4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16,
> 17], [18, 19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30, 31]]
 
> 
> That looks close to what you want?


That seems close, but off by a day?


— 
David Rock
da...@graniteweb.com




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


Re: [Tutor] how to get the weeks of a month

2019-03-04 Thread john fabiani



On 3/4/19 1:15 PM, David Rock wrote:

On Mar 4, 2019, at 13:19, Alan Gauld via Tutor  wrote:

On 04/03/2019 18:54, john fabiani wrote:


I need to print out the weeks of the month - given any month and any year.

I'm not totally clear how you define a week.

EDIT: OK I see the comment at the end now.


For example this month would have:

3/1/2019 - 3/3/2019   # notice that this a short week
3/4/2019 - 3/10/2019
3/11/2019 - 3/17/2019
3/18/2019 - 3/24/2019
3/25/2019 - 3/31/2019  # also this can be a short week as in April 2019
last week would be 4/29/2019 - 4/30-2019

What I think he’s shooting for is something similar to cal output

$ cal
  March 2019
Su Mo Tu We Th Fr Sa
 1  2
  3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31



So what I think you want is to

start with the first day and print each day up to Sunday.
Newline
print the current date up to sunday
newline
repeat until you run out of days in the month.



import calendar as cal
cal.monthcalendar(2019,3)

[[0, 0, 0, 0, 1, 2, 3], [4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16,
17], [18, 19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30, 31]]
That looks close to what you want?


That seems close, but off by a day?


—
David Rock
da...@graniteweb.com

I knew there was a simple why to get it done!  But where is it off my a day?

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


Re: [Tutor] how to get the weeks of a month

2019-03-04 Thread David Rock

> On Mar 4, 2019, at 15:28, john fabiani  wrote:
> 
> I knew there was a simple why to get it done!  But where is it off my a day?
> 

comparing

$ cal
 March 2019
Su Mo Tu We Th Fr Sa
1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31

to

import calendar as cal
cal.monthcalendar(2019,3)
[[0, 0, 0, 0, 1, 2, 3], [4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16,
17], [18, 19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30, 31]]

I see the first element of the array is
[0, 0, 0, 0, 1, 2, 3]

where I would have expected
[0, 0, 0, 0, 0, 1, 2]

Which I’m sure is just a question of “defining the start of the week” properly, 
but if you just took it as-is, Mar 1 would be Thursday, not Friday if you 
translated literally.


— 
David Rock
da...@graniteweb.com




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


Re: [Tutor] schedulers

2019-03-04 Thread Albert-Jan Roskam


On 28 Feb 2019 15:45, nathan tech  wrote:

Hi there,

I recently started working on a backup program, and the one big feature
everyone wants in backup programs is the ability to schedule backups, right?

but I'm thinking, should I do this?


==》 this might interest you: https://docs.python.org/3/library/sched.html. Or 
simply the Windows Task Scheduler?

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


Re: [Tutor] how to get the weeks of a month

2019-03-04 Thread john fabiani



On 3/4/19 1:35 PM, David Rock wrote:

On Mar 4, 2019, at 15:28, john fabiani  wrote:

I knew there was a simple why to get it done!  But where is it off my a day?


comparing

$ cal
  March 2019
Su Mo Tu We Th Fr Sa
 1  2
  3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31

to

import calendar as cal
cal.monthcalendar(2019,3)
[[0, 0, 0, 0, 1, 2, 3], [4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16,
17], [18, 19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30, 31]]

I see the first element of the array is
[0, 0, 0, 0, 1, 2, 3]

where I would have expected
[0, 0, 0, 0, 0, 1, 2]

Which I’m sure is just a question of “defining the start of the week” properly, 
but if you just took it as-is, Mar 1 would be Thursday, not Friday if you 
translated literally.


—
David Rock
da...@graniteweb.com
My understanding is - a week starts on Monday and ends on Sunday. So 
that looks correct.  Below I use a max function but I wonder if I should 
use a min function too.  Recall I am looking for the string of the dates 
for the week.


Here is my code:
import datetime
import calendar


#get the weeks of a month to get the dates to display
tday = datetime.datetime(2020,03,01)

weeksInMonth =calendar.monthcalendar(tday.year, tday.month)
lastdayof1stweek = weeksInMonth[0][6]
firstweek = tday.strftime("%m-%d_%Y")+ " - "+ 
datetime.datetime(tday.year, tday.month, 
lastdayof1stweek).strftime("%m-%d-%Y")

print firstweek
for i in range(len(weeksInMonth)):
    if i == 0:
    continue
    firstday = weeksInMonth[i][0]
    lastday =  max(weeksInMonth[i])
    weekstr = datetime.datetime(tday.year, tday.month, 
firstday).strftime("%m-%d-%Y") + ' - ' + datetime.datetime(tday.year, 
tday.month, lastday).strftime("%m-%d-%Y")

    print weekstr

def max(arr):
    max_ = arr[0]
    for item in arr:
    if item > max_:
    max_ = item
    return max_
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to get the weeks of a month

2019-03-04 Thread Ben Finney
john fabiani  writes:

> My understanding is - a week starts on Monday and ends on Sunday.

That's behaviour specific to a timezone. Which one are you using, and
does your program know to consult the timezone data for when a week
begins and ends?

https://www.timeanddate.com/calendar/days/>

-- 
 \ “I went to San Francisco. I found someone's heart.” —Steven |
  `\Wright |
_o__)  |
Ben Finney

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


Re: [Tutor] how to get the weeks of a month

2019-03-04 Thread David Rock

> On Mar 4, 2019, at 16:30, Ben Finney  wrote:
> 
> john fabiani  writes:
> 
>> My understanding is - a week starts on Monday and ends on Sunday.
> 
> That's behaviour specific to a timezone. Which one are you using, and
> does your program know to consult the timezone data for when a week
> begins and ends?


That’s why I said "Which I’m sure is just a question of “defining the start of 
the week” properly.”  “Properly” is in the eye of the beholder.  As long as 
it’s performing the way you expect it to perform, you should be fine.  If all I 
saw was the output you had, I’d think something was broken because I think in 
terms of first day being Sunday, so maybe include a note in the output what the 
First day of the week is if that’s appropriate?


— 
David Rock
da...@graniteweb.com




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


Re: [Tutor] how to get the weeks of a month

2019-03-04 Thread Alan Gauld via Tutor
On 04/03/2019 22:30, Ben Finney wrote:
> john fabiani  writes:
> 
>> My understanding is - a week starts on Monday and ends on Sunday.
> 
> That's behaviour specific to a timezone. Which one are you using, and
> does your program know to consult the timezone data for when a week
> begins and ends?

That's a whole different can of worms. Last time I checked there were
around 40 different timezones and only about 35 supported by most OS.
Some of them vary their rules year on year and their time intervals
can be as little as 20 minutes apart to 3 hours apart. Its a real
rats nest where local governance overrules standards at the drop
of a hat!

Fortunately you only have the days and months to worry about here!

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


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


Re: [Tutor] how to get the weeks of a month

2019-03-04 Thread john fabiani



On 3/4/19 2:47 PM, David Rock wrote:

On Mar 4, 2019, at 16:30, Ben Finney  wrote:

john fabiani  writes:


My understanding is - a week starts on Monday and ends on Sunday.

That's behaviour specific to a timezone. Which one are you using, and
does your program know to consult the timezone data for when a week
begins and ends?


That’s why I said "Which I’m sure is just a question of “defining the start of 
the week” properly.”  “Properly” is in the eye of the beholder.  As long as it’s 
performing the way you expect it to perform, you should be fine.  If all I saw was 
the output you had, I’d think something was broken because I think in terms of first 
day being Sunday, so maybe include a note in the output what the First day of the 
week is if that’s appropriate?


—
David Rock
da...@graniteweb.com



I didn't think about that.  I will in the future.
Thanks,
Johnf
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to get the weeks of a month

2019-03-04 Thread Mark Lawrence

On 04/03/2019 22:12, john fabiani wrote:



On 3/4/19 1:35 PM, David Rock wrote:

On Mar 4, 2019, at 15:28, john fabiani  wrote:

I knew there was a simple why to get it done!  But where is it off my 
a day?



comparing

$ cal
  March 2019
Su Mo Tu We Th Fr Sa
 1  2
  3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31

to

import calendar as cal
cal.monthcalendar(2019,3)
[[0, 0, 0, 0, 1, 2, 3], [4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16,
17], [18, 19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30, 31]]

I see the first element of the array is
[0, 0, 0, 0, 1, 2, 3]

where I would have expected
[0, 0, 0, 0, 0, 1, 2]

Which I’m sure is just a question of “defining the start of the week” 
properly, but if you just took it as-is, Mar 1 would be Thursday, not 
Friday if you translated literally.



—
David Rock
da...@graniteweb.com

My understanding is - a week starts on Monday and ends on Sunday.


From 
https://docs.python.org/3/library/calendar.html#calendar.setfirstweekday


calendar.setfirstweekday(weekday)

Sets the weekday (0 is Monday, 6 is Sunday) to start each week. The 
values MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, and 
SUNDAY are provided for convenience. For example, to set the first 
weekday to Sunday:


import calendar
calendar.setfirstweekday(calendar.SUNDAY)

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

Mark Lawrence

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