Python Forum
What is wrong with the code?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is wrong with the code?
#1
Hi all,

I received an email the other day requesting me to verify some code.
The code was this...
import _winreg


class RegistryError(Exception):
    def __init__(self, value):
        self.value = value
    def __str__(self):
        return repr(self.value)


def getRegistryFoldersUnderParent(registry_connection, parent_folder):
    all_folders = ""
    
    try:
        if registry_connection == "machine":
            a_reg = _winreg.ConnectRegistry(None,_winreg.HKEY_LOCAL_MACHINE)
        elif registry_connection == "user":
            a_reg = _winreg.ConnectRegistry(None,_winreg.HKEY_CURRENT_USER)         
        else:
            raise RegistryError("Unknown registry connection type %s. Must be 'machine' or 'user'" % registry_connection)

        parentKey = _winreg.OpenKey(a_reg, parent_folder)
        
    except WindowsError as ex:
        raise RegistryError(ex)
  
# Start on the second entry, so i=1 at start
    i = 0
    while True:
       try:
           
           key = _winreg.EnumKey(parentKey, i)
           all_folders+=key + " ;"
           i += 1
       except WindowsError as ex:
           break

    all_folders = all_folders[:-2]

    return all_folders
I was asked what does the code do? What is wrong with it? and are there any improvements to be made?

Honestly, I'm a total beginner at this kind of thing.
It looks to me as though the code is to determine between an automated and user connection but I wouldn't be surprised to find out I'm totally wrong.
I think near the end there's a : that should be a ; but then aside from that, I can't really find any obvious issues.

I was hoping for some guidance or advice really. Somewhere I can start from to solve the problem and answer the questions.

Thanks,
Reply
#2
Quote:What is wrong with it?
The easiest way is to run it and find out if there are syntax errors. Of course you need to make an object and run each method. You should go line by line to determine what control is doing. Print variables to find out values as control moves. Your professor should have given you the knowledge to improve upon it i they require it. 

I cant run it as i am on linux.
Recommended Tutorials:
Reply
#3
It connects to Windows registry machine or user folder and returns all subkeys. I do not use Windows either but this says in the documentation
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
(Jan-25-2017, 03:34 AM)jackson111 Wrote: I think near the end there's a : that should be a ; but then aside from that, I can't really find any obvious issues.

Hi there,

This is the kind of thing they give out for technical job interviews, I've had a few electronics pre interview  questions like this. Newish to programming myself, but I thought I'd throw in my thoughts on this line of your post.

So my experience of python is that it is colons ':' that get used mainly, semi colons (;) on the whole don't seem to be used much in python other than as separators. On this occasion, I would argue that all the colons here are correct and suspect that the one you are referring to is the one in the [:-2] which is standard slice notation for lists the one semi colon in the code, is in quotes, so will be part of the output either for human readable or to format the output in something like JSON format for machine readable.

Other observations just from looking at the code, are that it is unclear as to whether the code after the comment line is still part of the function, the comment is not indented, this is just an assumption, but I comment sections using the same indentation as the block being commented, so here the lack of indentation of the comment gives question as to whether the next part of the code is part of the previous function or not, so is this one of the errors?

All_folders, is it a namespace for a string or a list? it is first set up as an empty string, which is later populated as a string, then later in the code,  the same variable is being used for a list. Surely even if that variable has done it's duty, is there really a need to recycle a variable in this way, for a different usage and using a different data type? You might want to recycle a variable because you are going through a batch of things and it does not need to perpetuate, but to reuse in a completely different way is a bit strange and makes reading the code a little tricky.

I would take a guess that this might be a function for validating and logging a remote connection to a windows server. Hope this helps somewhere along the line
Reply
#5
(Jan-25-2017, 03:34 AM)jackson111 Wrote: Hi all,

I received an email the other day requesting me to verify some code.
The code was this...

Why are you responding to clearly spam email?  Have you no self respect?
Reply
#6
(Feb-03-2017, 11:01 PM)nilamo Wrote:
(Jan-25-2017, 03:34 AM)jackson111 Wrote: Hi all,

I received an email the other day requesting me to verify some code.
The code was this...

Why are you responding to clearly spam email?  Have you no self respect?

Spam, perhaps not... But since the kneejerk reaction of a programmer is to execute the code to see what is wrong with it, the recipient may be running some unknown and duboius code against the registry of his/her machine and utterly destroy it. So, a pretty good prank :)
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  im not sure what ive done wrong code doesnt run dgizzly 3 1,357 Nov-16-2022, 03:02 AM
Last Post: deanhystad
  what is wrong with my code 53535 4 1,528 Apr-07-2022, 11:37 AM
Last Post: 53535
  What's wrong with my code? NeedHelpPython 4 2,195 Oct-22-2021, 07:59 PM
Last Post: Yoriz
  Help with my code due 11:59 pm, can you tell me where I went wrong and help fix it? shirleylam852 1 2,653 Dec-09-2020, 06:37 AM
Last Post: stranac
  I am getting an incorrect average, and not sure why? What's wrong with my code? shirleylam852 8 4,660 Nov-20-2020, 05:32 AM
Last Post: deanhystad
  Something is Wrong with my code susmith552 4 3,031 Nov-28-2019, 02:16 AM
Last Post: susmith552
  What is wrong with my code? Than999 1 2,372 Nov-10-2019, 08:59 PM
Last Post: ichabod801
  Wrong output on my code. JTNA 2 7,888 Apr-04-2019, 01:55 PM
Last Post: JTNA
  Why is this code wrong? Lemmy 4 5,177 Apr-05-2018, 03:46 PM
Last Post: Lemmy
  What's wrong with my code and visuals for python? beginnercoder04 2 2,800 Mar-17-2018, 01:06 AM
Last Post: beginnercoder04

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020