Question about pickle

2013-06-25 Thread Phu Sam
I have a method that opens a file, lock it, pickle.load the file into a
dictionary.
I then modify the status of a record, then pickle.dump the dictionary back
to the file.

The problem is that the pickle.dump never works. The file never gets
updated.

def updateStatus(self, fp, stn, status):
  f = open(fp, 'rw+')
  fcntl.flock(f.fileno(),fcntl.LOCK_EX | fcntl.LOCK_NB)

  tb = pickle.load(f)

  self.modifyDict(tb, stn, status)

  pickle.dump(tb, f)

  fcntl.flock(f.fileno(),fcntl.LOCK_UN)
  f.close()


What could be the problem here?
What mode should I use to open the file to allow both pickle.load and
pickle.dump?

Thanks,


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


Re: Question about pickle

2013-06-26 Thread Phu Sam
f.seek(0)  really does the trick.

Danke sehr,

Phu


On Tue, Jun 25, 2013 at 6:47 AM, Peter Otten <__pete...@web.de> wrote:

> Phu Sam wrote:
>
> > I have a method that opens a file, lock it, pickle.load the file into a
> > dictionary.
> > I then modify the status of a record, then pickle.dump the dictionary
> back
> > to the file.
> >
> > The problem is that the pickle.dump never works. The file never gets
> > updated.
> >
> > def updateStatus(self, fp, stn, status):
> >   f = open(fp, 'rw+')
> >   fcntl.flock(f.fileno(),fcntl.LOCK_EX | fcntl.LOCK_NB)
> >
> >   tb = pickle.load(f)
> >
> >   self.modifyDict(tb, stn, status)
>
> f.seek(0)
>
> >   pickle.dump(tb, f)
> >
> >   fcntl.flock(f.fileno(),fcntl.LOCK_UN)
> >   f.close()
> >
> >
> > What could be the problem here?
>
> pickle.load() moves the file position to the end of the (first) pickle.
> pickle.dump() writes the modified dict starting at the current position.
> You
> end up with two versions of the dict, but you'll only ever read the first.
>
> The fix is to go back to the start of the file with f.seek().
>
> > What mode should I use to open the file to allow both pickle.load and
> > pickle.dump?
>
> Assuming that the file already exists when updateStatus() is invoked for
> the
> first time: "r+b".
>
> I usually open the file twice, once for reading and then for writing, but I
> guess that would interfere with locking.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to 'ignore' an error in Python?

2023-04-29 Thread Phu Sam
Unsubscribe

On Sat, Apr 29, 2023 at 7:05 PM Chris Angelico  wrote:

> On Sun, 30 Apr 2023 at 11:58, Chris Green  wrote:
> >
> > Chris Angelico  wrote:
> > > On Sat, 29 Apr 2023 at 14:27, Kushal Kumaran 
> wrote:
> > > >
> > > > On Fri, Apr 28 2023 at 04:55:41 PM, Chris Green  wrote:
> > > > > I'm sure I'm missing something obvious here but I can't see an
> elegant
> > > > > way to do this.  I want to create a directory, but if it exists
> it's
> > > > > not an error and the code should just continue.
> > > > >
> > > > > So, I have:-
> > > > >
> > > > > for dirname in listofdirs:
> > > > > try:
> > > > > os.mkdir(dirname)
> > > > > except FileExistsError:
> > > > > # so what can I do here that says 'carry on regardless'
> > > > > except:
> > > > > # handle any other error, which is really an error
> > > > >
> > > > > # I want code here to execute whether or not dirname exists
> > > > >
> > > > >
> > > > > Do I really have to use a finally: block?  It feels rather clumsy.
> > > > >
> > > > > I suppose I could test if the directory exists before the
> os.mkdir()
> > > > > but again that feels a bit clumsy somehow.
> > > > >
> > > > > I suppose also I could use os.mkdirs() with exist_ok=True but again
> > > > > that feels vaguely wrong somehow.
> > > > >
> > > >
> > > > Why does exist_ok=True feel wrong to you?  This is exactly what it is
> > > > there for.
> > > >
> > >
> > > Using mkdirs when you only want to make one is inviting problems of
> > > being subtly wrong, where it creates too many levels of directory.
> > > Personally, I would just do:
> > >
> > > try: os.mkdir(dirname)
> > > except FileExistsError: pass
> > >
> > > and not try to handle anything else at all.
> > >
> > Yes, OP here, that seems to me to be the 'right' way to do it.
> > Basically I hadn't realised the effect of pass in a try block and
> > that's why I asked the question originally.
> >
>
> There's two points to note here. "pass" doesn't do anything
> whatsoever; it's only there to prevent the syntactic problem of having
> nothing in that block. This will also suppress the error:
>
> try:
> os.mkdir(dirname)
> except FileExistsError:
> dummy = "ignore"
>
> The second thing is that, as soon as you have an "except" clause that
> matches the error, that's it - it stops there. The error is considered
> handled at that point. If that's NOT what you want, you have a few
> options. Firstly, you can simply not have a matching except clause.
> That's why we like to be as precise as possible with our catching;
> every other type of problem will be left uncaught. Secondly, you can
> use "try/finally" to add code that happens as the exception flies by,
> but doesn't catch it (it also happens at the end of the block for
> other reasons). And thirdly, you can reraise the exception:
>
> try:
> os.mkdir(dirname)
> except FileExistsError:
> print("Hey, that one already exists!")
> raise
>
> That's going to keep the exception going just as if it hadn't been
> caught, but with the additional handling.
>
> But if you don't do any of those things, the exception is deemed to be
> handled, and it goes no further.
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Scraping multiple web pages help

2019-02-27 Thread Phu Sam
You need to obtain a key for API first - from
https://regulationsgov.github.io/developers/

The Regulations.gov API is taking action to conserve system resources.
Beginning immediately, we will limit
access to one account per organization, and require approval for enabling
accounts.* Please contact the *
*Regulations.gov Help Desk, if you would like to request an API key. *
Please provide your name, email  address, organization, and intended use of
the API.

Send email to:
regulati...@erulemakinghelpdesk.com

Good luck,

Phu

On Mon, Feb 18, 2019 at 10:19 AM Drake Gossi  wrote:

> Hello everyone,
>
> For a research project, I need to scrape a lot of comments from
> regulations.gov
>
>
> https://www.regulations.gov/docketBrowser?rpp=25&so=DESC&sb=commentDueDate&po=0&dct=PS&D=ED-2018-OCR-0064
>
> But partly what's throwing me is the url addresses of the comments. They
> aren't consistent. I mean, there's some consistency insofar as the numbers
> that differentiate the pages all begin after that 0064 number in the url
> listed above. But the differnetiating numbers aren't even all the same
> amount of numbers. Some are 4 (say, 4019) whereas others are 5 (say,
> 50343). But I dont think they go over 5. So this is a problem. I dont know
> how to write the code to access the multiple pages.
>
> I should also mention I'm new to programing, so that's also a problem (if
> you cant already tell by the way I'm describing my problem).
>
>
> I should also mention that, I think, there's an API on regulations.gov,
> but
> I'm such a beginner that I dont evem really know where to find it, or even
> what to do with it once I do. That's how helpless am right now.
>
> Any help anyone could offer would be much appreciated.
>
> D
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Dictionary

2019-02-27 Thread Phu Sam
The condition  'if not fibs.get(n):'  will not work because
n = 0
  fibs.get(0) is 0 so not 0 is 1

Here is the modified code that works:

fibs={0:0,1:1}
def rfib(n):
  if n == 0 or n == 1:
  return fibs[n]
  else:
fibs[n]=rfib(n-2)+rfib(n-1)
return fibs[n]

>>> rfib(8)
21
>>> fibs
{0: 0, 1: 1, 2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21}


Cheers,

Phu





On Sun, Feb 24, 2019 at 10:56 AM Himanshu Yadav <
himanshuyadav9630994...@gmail.com> wrote:

> fibs={0:0,1:1}
> def rfib(n):
>   global fibs
>if not fibs.get(n):
> fibs[n]=rfib(n-2)+rfib(n-1)
> return fibs[n]
>
> Why it is gives error??
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list