User Controls

CODE 👏 REVIEW 👏

  1. #1
    Sophie Pedophile Tech Support
    Welcome to Code Review. T&T's favorite thread. If you got some code and would like it reviewed you post it here. I promise this is not some clever ploy to steal your ideas. It's going to go like this you post some code...


    class COMCollection(COMObject):
    """Abstract base class which implements Count, Item, and _NewEnum."""
    def __init__(self, itemtype, collection):
    self.collection = collection
    self.itemtype = itemtype
    super(COMCollection, self).__init__()

    def _get_Item(self, this, pathname, pitem):
    if not pitem:
    return E_POINTER
    item = self.itemtype(pathname)
    return item.IUnknown_QueryInterface(None,
    pointer(pitem[0]._iid_),
    pitem)

    def _get_Count(self, this, pcount):
    if not pcount:
    return E_POINTER
    pcount[0] = len(self.collection)
    return S_OK

    def _get__NewEnum(self, this, penum):
    if not penum:
    return E_POINTER
    enum = VARIANTEnumerator(self.itemtype, self.collection)
    return enum.IUnknown_QueryInterface(None,
    pointer(IUnknown._iid_),
    penum)


    And we'll review it like so...

    Shouldn't this be a mixin class?
    Isn't this class borked anyway?

    And then we'll go NEXT 👏 CODE 👏

    I'll chime in as long as i can read the lang you're posting in. That first one was a practice round i yanked that straight out of a library.

    Ok is this retarded?


    def collect(self):
    outfile = open("data.txt", "a")
    outfile.write("\n")
    outfile.write("-"*15)
    outfile.write("\n")
    outfile.write(self.tracked_string_concat)

    with ZipFile("data.zip", "w") as zip:
    zip.write(outfile)
    outfile.close()



    It's part of a class don't worry about it, also the last three lines is all i care about really. I am importing the zipfile lb like so:


    from zipfile import *


    I could probably figure out how to do it properly but i just wrote a whole bunch of crap that this is a part of and i don't feel like debugging duty, but i got the sneaking suspicion it's gonna have to be debugged ANYWAY. CODE 👏 REVIEW 👏

    Post your own code for a review.

    NEXT 👏 CODE 👏
  2. #2
    filtration African Astronaut
    This post has been edited by a bot I made to preserve my privacy.
  3. #3
    Sophie Pedophile Tech Support
    Originally posted by filtration
    rm -rf /

    Buggy! You forgot


    sudo


    and maybe


    --no-preserve-root


    NEXT 👏 CODE 👏
    The following users say it would be alright if the author of this post didn't die in a fire!
  4. #4
    filtration African Astronaut
    This post has been edited by a bot I made to preserve my privacy.
  5. #5
    Sophie Pedophile Tech Support
    Originally posted by filtration Don't need either… Try it.

    You shouldn't be running as root at all times, that's a fire hazard.
    The following users say it would be alright if the author of this post didn't die in a fire!
  6. #6
    filtration African Astronaut
    This post has been edited by a bot I made to preserve my privacy.
  7. #7
    Lanny Bird of Courage
    I don't really do windows programming but this:


    def _get_Count(self, this, pcount):
    if not pcount:
    return E_POINTER
    pcount[0] = len(self.collection)
    return S_OK


    looks very strange to me and would be considered non-idiomatic, at least in just normal old python. Functions taking lists and mutating them is... undesirable, although sometimes necessary for performance reasons. Taking a ref type argument (like a list), mutating a single element in it (constant time) then returning a status code is wrong unless it's necessary to play with C integration or maybe the win32 API. It's a very C way of doing things just because of the way that language is designed. If you're the only one calling this method then you could return the count and raise an exception in the error case.

    Originally posted by Sophie Ok is this retarded?


    def collect(self):
    outfile = open("data.txt", "a")
    outfile.write("\n")
    outfile.write("-"*15)
    outfile.write("\n")
    outfile.write(self.tracked_string_concat)

    with ZipFile("data.zip", "w") as zip:
    zip.write(outfile)
    outfile.close()


    This isn't retarded but you don't need to write out the "data.txt" file and worry about the locking/releasing that comes with that (e.g. in this case if zip.write() throws outfile won't be closed).

    Instead I would write it like:


    def collect(self):
    # create zipped file contents
    contents = "\n%s\n%s" % ('-'*15, self.tracked_string_concat)

    with ZipFile("data.zip", "w") as zip:
    zip.writestr('data.txt', contents)


    This way you never have to dump the content to disk just to read it back into memory to zip it and you don't have to manage the 'data.txt' file.
    The following users say it would be alright if the author of this post didn't die in a fire!
  8. #8
    Admin African Astronaut
    System.out.println("Fu");
  9. #9
    WellHung Black Hole
    This thread is for far brighter minds than my own... its like im trying to read another language.
  10. #10
    aldra JIDF Controlled Opposition
    Originally posted by WellHung This thread is for far brighter minds than my own… its like im trying to read another language.

    it's not difficult if you're willing to put some time in to learn.

    the first one's always the hardest because computer logic isn't the same as logic in your everyday life, but once you learn that everything else falls into place
    The following users say it would be alright if the author of this post didn't die in a fire!
  11. #11
    WellHung Black Hole
    Originally posted by aldra it's not difficult if you're willing to put some time in to learn.

    the first one's always the hardest because computer logic isn't the same as logic in your everyday life, but once you learn that everything else falls into place

    Do you have natural aptitudes with this type of thing? Or was your learning curve steep, in this regard?
  12. #12
    aldra JIDF Controlled Opposition
    I started young so it was probably easier, but on the other hand I didn't have all the internet resources that are available today (would've been late 90s I think). It's always difficult to learn a new paradigm though I guess.

    The language they're using up there is Python, if you look over a quick tutorial or two you'll probably be able to make out what they're trying to do
  13. #13
    aldra JIDF Controlled Opposition
    on topic:


    use strict;
    use warnings;

    use JSON;
    use Data::Dumper;
    my $mvColour;


    $| = 1; # don't buffer
    print scalar <STDIN>; # skip first two lines, they're junk
    print scalar <STDIN>;
    while (my ($statusline) = (<STDIN> =~ /^,?(.*)/))
    {
    my @blocks = @{decode_json($statusline)};
    @blocks =
    ({
    full_text => getMullvadStatus(),
    name => 'mullvad',
    color => $mvColour
    }, @blocks);
    print encode_json(\@blocks) . ",\n";
    }

    sub getMullvadStatus
    {
    my $mvRaw=lc(`mullvad status`);
    my @mvData=split(/\n/,$mvRaw);
    my @mvStatus=split(/\: /,$mvData[0]);

    my $mvRecords=@mvData;
    my $mvAttempts=1;
    my $finalStatus='mullvad: ';


    if($mvStatus[1] eq 'connected')
    {
    $finalStatus.='connected';
    $mvColour='#3465A4';
    }
    else
    {
    $finalStatus.='disconnected';
    $mvColour='#383830';
    }
    $mvRecords=@mvData;
    $mvAttempts++;
    return($finalStatus);
    }


    first thing I've written in a while, it takes a json list of items (via pipe), gets my current vpn status and adds it to the list.

    it's for the i3status bar, based on one of their examples



    ***oh yeah, perl
  14. #14
    WellHung Black Hole
    Originally posted by aldra I started young so it was probably easier, but on the other hand I didn't have all the internet resources that are available today (would've been late 90s I think). It's always difficult to learn a new paradigm though I guess.

    The language they're using up there is Python, if you look over a quick tutorial or two you'll probably be able to make out what they're trying to do

    ill give it a look-see... It would be nice to be able to have some basic comprehension of what's going on here. ty for the info.👍
  15. #15
    Bueno motherfucker
Jump to Top