Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8177105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:25:39+00:00 2026-06-06T23:25:39+00:00

Have a look at the following method: def load_ad_mediums_from_file(self, filename ): file = io.open(

  • 0

Have a look at the following method:

def load_ad_mediums_from_file(self, filename ):
    file = io.open( filename, "r" )
    ad_mediums = {}
    for line in file:
        if len(line) > 0 and line[0] != 'a':
            parts = line.strip().split(";")
            ad_medium = Objects.Ad_Medium()
            ad_medium.id = int( parts[0] )
            for i in range(1,8):
                cat_parts = parts[i].strip().split(",")
                category_id = int(cat_parts[0]);
                ad_medium.categories[category_id] = float(cat_parts[1])
                ad_medium.impressions[category_id] = int(cat_parts[2]) 

            ad_mediums.update( { ad_medium.id : ad_medium } )
    file.close()
    return ad_mediums

The problem: Althoug each line in the file is different the values in the ad_mediums dictionary have all the same values. To me it looks like as if the creation of a new object “ad_medium = Objects.Ad_Medium()” has no effect but just simply overwrites the values from the object of the previous loop.

How can I fix this?

EDIT: The Ad_Medium class

class Ad_Medium(object):

id = 0
categories = { 1:0.0, 2:0.0, 3:0.0, 4:0.0, 5:0.0, 6:0.0, 7:0.0 }
impressions = { 1:0, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0  } 

def __str__(self):
    string = str(self.id) + ";"
    for i in range(1,8):
        string += str(i) + "," + str( self.categories[i] ) + "," + str( self.impressions[i] ) + ";"
    return string[:-1] + "\n"



def normalize_categories_with_impressions(self ):
    impressions = 0.0
    for i in range(1,8):
        impressions += float(self.impressions[i])

    if impressions == 0.0: 
        return

    for i in range(1,8):
        self.categories[i] = float(self.categories[i])/float(impressions)



def reset_categories(self):
    for i in range(1,8):
        self.categories[i] = 0
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:25:40+00:00Added an answer on June 6, 2026 at 11:25 pm

    In your class definition, id, categories and impressions are class attributes (shared by all instance). The ad_medium.id = int( parts[0] ) statement defines an ‘id’ instance attribute, but all the operations on categories and impressions (which are not rebound but mutated) work on the class attributes. You want your class definition to have an initializer method where you create the instance attributes, ie :

    class Ad_Medium(object):
        def __init__(self):
            self.id = 0
            self.categories = { 1:0.0, 2:0.0, 3:0.0, 4:0.0, 5:0.0, 6:0.0, 7:0.0 }
            self.impressions = { 1:0, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0  } 
    

    I strongly suggest that you learn more about Python’s object model, attribute lookup rules etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following destroy method in my Tracks controller: def destroy if params[:product_id].present?
Have a look at the following code: class A(object): defaults = {'a': 1} def
Have a look at the following Scala example: class A { def foo(x: Int):
I have HttpClient 4.1. Please have a look at following program: import org.apache.http.client.methods.*; import
Have a look at following scenario: public class ParentClass { private Integer testVar =
Have a look at the following code you are not required to read the
please have a look at the following code import java.util.ArrayList; import java.util.List; public class
Please have a look at the following code namespace Funny { class QuesionsAndAnswers {
Please have a look at the following code: $(#saveButton).click(function(){ $this = $(#tableData).find(input:checked).parent().parent(); tea =
Please have a look at the following machine code ‎0111001101110100011100100110010101110011011100110110010101100100 This means something. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.