Multiple calls

20 views
Skip to first unread message

Soumen Khatua

unread,
Feb 17, 2020, 1:32:00 PM2/17/20
to django...@googlegroups.com
Hi Folks,
Actually I want to load the csv file for first time only afterwards I don't want to load the csv files,but everytime when i'm calling the url the same csv fie is loading for the same operation.How I can restrict it for multiple loadings?
Here is my code:
class AutocompleteView(APIView):
    def __init__(self):
        self.keys = []
        with open("search_terms_converteds.csv",'r') as file:
            reader = csv.reader(file)
            for row in reader:
                self.keys.append(row[1])
            print("constructor execution")

        global t
        t = Trie()
        t.formTrie(self.keys)

    def post(self,request):
        key = request.data
        print("key:",key)
        suggested_word = t.printAutoSuggestions(key)
        return Response(suggested_word, status = status.HTTP_200_OK)

Thank you in advance

Regards,
Soumen


Kasper Laudrup

unread,
Feb 17, 2020, 1:45:15 PM2/17/20
to django...@googlegroups.com
Hi Soumen,

On 17/02/2020 14.31, Soumen Khatua wrote:
> Hi Folks,
> Actually I want to load the csv file for first time only afterwards I
> don't want to load the csv files,but everytime when i'm calling the url
> the same csv fie is loading for the same operation.How I can restrict it
> for multiple loadings?

As a quick and dirty solution, you could probably use a class variable:

https://docs.python.org/3/tutorial/classes.html#class-and-instance-variables

But a nicer way would be to have some kind of service or similar that
the view could call or perhaps abstract the CSV file as a Django model.

Kind regards,

Kasper Laudrup

Soumen Khatua

unread,
Feb 17, 2020, 1:52:52 PM2/17/20
to django...@googlegroups.com
Okay.
Is it possible to load the csv file one time do the POST or any request multiple times without loading the same file.Of course If it is in same class??


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/cdb81c92-9cd9-7401-ef86-67c9fc643000%40stacktrace.dk.

Kasper Laudrup

unread,
Feb 17, 2020, 1:58:05 PM2/17/20
to django...@googlegroups.com
Hi Soumen,

On 17/02/2020 14.51, Soumen Khatua wrote:
> Okay.
> Is it possible to load the csv file one time do the POST or any request
> multiple times without loading the same file.Of course If it is in same
> class??
>

Yes, I just gave you some ideas on how that could be done.

Kind regards,

Kasper Laudrup

Soumen Khatua

unread,
Feb 17, 2020, 2:01:06 PM2/17/20
to django...@googlegroups.com
Yeah, I'm trying to do it in your way but somehow it's not working in my script.

Thanks for your valuable time.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

tribhuvan kishor

unread,
Feb 17, 2020, 2:11:29 PM2/17/20
to django...@googlegroups.com
i faced the same problem while i was dealing with CSV file.
i gave a unique file in csv to make a check on the particular entry. by this way, i avoided multiple entries in my database




--
regards 
Tribhuvan Kishor Bhaskar
9818078761

tribhuvan kishor

unread,
Feb 17, 2020, 2:11:55 PM2/17/20
to django...@googlegroups.com
unique field in CSV

Soumen Khatua

unread,
Feb 17, 2020, 2:19:54 PM2/17/20
to django...@googlegroups.com
Could you send me any code snippet or any link,Please?

Thank you for your response



tribhuvan kishor

unread,
Feb 17, 2020, 2:34:04 PM2/17/20
to django...@googlegroups.com
add a unique field in csv and 

class AutocompleteView(APIView):
    def __init__(self):
        self.keys = []
        with open("search_terms_converteds.csv",'r') as file:
            reader = csv.reader(file)
            for row in reader:
               if row.get("perticular unique id field").exist():
                    print("row already exist")
                else:

                self.keys.append(row[1])
            print("constructor execution")

        global t
        t = Trie()
        t.formTrie(self.keys)

    def post(self,request):
        key = request.data
        print("key:",key)
        suggested_word = t.printAutoSuggestions(key)
        return Response(suggested_word, status = status.HTTP_200_OK)

tribhuvan kishor

unread,
Feb 17, 2020, 2:36:41 PM2/17/20
to django...@googlegroups.com
its possible if manipulation possible in CSV

Soumen Khatua

unread,
Feb 17, 2020, 2:41:48 PM2/17/20
to django...@googlegroups.com
Actually my requirement is different it's like If anyone call the this class first time then entire script will be execute. Afterwards if any one wants to POST or any request then the constructor should not be execute only the specefied request will be execute. otherwise if the csv file will load each and everytime then it will take lots of time.

Thank you

maninder singh Kumar

unread,
Feb 17, 2020, 5:10:49 PM2/17/20
to django...@googlegroups.com
Hi Soumen,
Where is the Csv file loading ?
You could try return redirect also if the problem is due to continued resubmission.

Sent from my iPad
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/cdb81c92-9cd9-7401-ef86-67c9fc643000%40stacktrace.dk.

Soumen Khatua

unread,
Feb 17, 2020, 5:15:41 PM2/17/20
to django...@googlegroups.com
The CSV file is in project folder.Could you send me any code snippet,Please?

Thanks for your time.



Soumen Khatua

unread,
Feb 17, 2020, 5:17:38 PM2/17/20
to django...@googlegroups.com
the CSV file is loading inside the AutoComplete(class) constructor

Kasper Laudrup

unread,
Feb 17, 2020, 5:30:51 PM2/17/20
to django...@googlegroups.com
Hi Soumen,

On 17/02/2020 14.59, Soumen Khatua wrote:
> Yeah, I'm trying to do it in your way but somehow it's not working in my
> script.
>

Which way?

I didn't give you any way to do it, I gave you three ideas and I would
be very interested to hear what would work out best.

I definitely find the "class member/singleton" kind of way a dirty hack
that I would personally avoid (views shouldn't have any state in the
first place).

But it would be very interesting to hear how you've approached the
service and custom model ideas.

Would you care to share the code where you've tried that approach?

Kind regards,

Kasper Laudrup
Reply all
Reply to author
Forward
0 new messages