Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > fr.comp.lang.python > #3332

Re: storing and reading notes in python

Newsgroups fr.comp.lang.python
Date 2020-04-13 11:50 -0700
References <e411f9e8-bc83-401e-bac3-a47a08d74fe2@googlegroups.com>
Message-ID <c0edbba7-ae6c-4c54-8884-6357d8def85c@googlegroups.com> (permalink)
Subject Re: storing and reading notes in python
From john Stone <profesor.fir@gmail.com>

Show all headers | View raw


W dniu niedziela, 12 kwietnia 2020 15:43:22 UTC+2 użytkownik john Stone napisał:
> salute,
> 
> i can weakly read french (im learning it) but 
> i am unable  to write as for now, so maybe let me ask
> this question in english but you may answer
> in french 
> 
> im experienced in c but im total noob in python,
> maybe you can give me some hints
> 
> i write a irc bot (nicknamed 'minion') in python,
>  i want to make such  thing, if user will 
> send a line like
> 
> <fir> minion invite emily = hello emilly minion welcomes you
> 
> this line is stored in text file fir.txt as a line
> 
> invite emily = hello emilly minion welcomes you
> 
> then if i will send a line
> 
> <fir> minion invite emily
> then minion will send
> <minion> hello emilly minion welcomes you
> 
> obvoiusly linnes should be added and if same 'key' found 
> (in that case key is "invite emily") then it should be
> replaced
> 
> i also would want to mke it with some sense not to make 
> bery much disk reads maybe
> 
> i know python like 3 days and dont see how to do it yet
> 
> can someone help, tnx

ok i found it

   p = ircmsg.find(channel) 
    r = ircmsg.find('===') 
    if p!=-1 and r!=-1: 
      key = ircmsg[p+len(channel)+1:r] 
      value = ircmsg[r+1+1+1:] 
      say("przyjalem") 
      bot_memory[key]=value 
      save_bot_memory() 

    p = ircmsg.find(channel) 
    if p!=-1: 
      key = ircmsg[p+len(channel)+1:] 
      if key in bot_memory: 
        say(bot_memory[key]) 

where 


import json 

bot_memory = {'a': 'a', 
              'b': 'b', 
              'c': 'c'} 

f = open("bot_memory.json", 'r') 
bot_memory = json.load(f) 
f.close() 

def save_bot_memory(): 
    j = json.dumps(bot_memory) 
    f = open("bot_memory.json","w") 
    f.write(j) 
    f.close() 
    return 

i hope this is ok

Back to fr.comp.lang.python | Previous | NextPrevious in thread | Find similar


Thread

storing and reading notes in python john Stone <profesor.fir@gmail.com> - 2020-04-12 06:43 -0700
  Re: storing and reading notes in python john Stone <profesor.fir@gmail.com> - 2020-04-13 11:50 -0700

csiph-web