class Write():
    def __init__(self):
        self.pencil = Pencil(pencilLength=4, eraserDurability=10)
        self.paper = ''

    def write_on_paper(self, desired_text_to_write):
        text_to_write = self.pencil.pencil_write(desired_text_to_write)
        self.paper += text_to_write

    def sharpen_pencil(self):
        self.pencil.pencil_sharpen()

    def erase(self, desired_text_to_erase):
        text_to_erase = self.pencil.pencil_erase(
            text_to_erase=desired_text_to_erase)
        index = self.paper.rfind(desired_text_to_erase)
        self.paper = self.paper[:index] + text_to_erase + self.paper[
            index + len(text_to_erase):]
        return index

    def edit(self, index, text_to_add):
        current_text = self.paper[index:index + len(text_to_add)]
        for i in range(0, len(text_to_add)):
            if current_text[i] != text_to_add[i]:
                if not current_text[i].isspace(
                ) and not text_to_add[i].isspace():
                    text_to_add = text_to_add[:i] + '@' + text_to_add[i + 1:]
                else:
                    text_to_add = text_to_add[:i] + text_to_add[
                        i] + text_to_add[i + 1:]
        text_to_write = self.pencil.pencil_write(text_to_add)
        self.paper = self.paper[:index] + text_to_write + self.paper[
            index + len(text_to_add):]
Beispiel #2
0
 def test_sharpen_pencil_decrease_length_by_one(self):
     p = Pencil(pencilLength=4, eraserDurability=4)
     new_text = p.pencil_write('abcdef')
     self.assertEqual(p.pointDurability, 39994)
     p.pencil_sharpen()
     self.assertEqual(p.pointDurability, 40000)
     self.assertEqual(p.pencilLength, 3)
Beispiel #3
0
    def test_pencil_should_write_text(self):
        pencil = Pencil()

        text = "What possessed the first man to ride a horse?"
        pencil.write(self.paper, text)

        self.assertEqual(text, self.paper.read())
Beispiel #4
0
def test_write_throws_when_given_non_string_text():
    pencil = Pencil()
    raisedException = False
    try:
        pencil.write(7)
    except ValueError:
        raisedException = True
    assert (raisedException)
Beispiel #5
0
 def test_whitespaceDoesntDepleteErasers(self):
     p = Pencil(length=40, pointDurability=1000, eraserDurability=5)
     p.write("last on last night's schedule was immigration law")
     p.erase(" was ")
     self.assertTrue(
         p.getText() == "last on last night's schedule     immigration law")
     p.erase("la")
     self.assertTrue(
         p.getText() == "last on last night's schedule     immigration   w")
Beispiel #6
0
 def test_erasersWearOut(self):
     p = Pencil(length=40, pointDurability=1000, eraserDurability=2)
     p.write("last on last night's schedule was immigration law")
     p.erase("la")
     self.assertTrue(
         p.getText() == "last on last night's schedule was immigration   w")
     p.erase("la")
     self.assertTrue(
         p.getText() == "last on last night's schedule was immigration   w")
Beispiel #7
0
 def test_erasingTextReplacesItsLastOccurrenceWithSpaces(self):
     p = Pencil(length=40, pointDurability=1000, eraserDurability=200)
     p.write("last on last night's schedule was immigration law")
     p.erase("la")
     self.assertTrue(
         p.getText() == "last on last night's schedule was immigration   w")
     p.erase("la")
     self.assertTrue(
         p.getText() == "last on   st night's schedule was immigration   w")
Beispiel #8
0
def test_once_sharpened_to_length_0_it_does_not_restore_point_durability():
    pencil = Pencil(5, 1)
    assert pencil.write('Texts') == 'Text '
    pencil.sharpen()
    assert pencil.write('Texts') == 'Text '
    pencil.sharpen()
    assert pencil.write('Texts') == '     '
Beispiel #9
0
 def test_write_max_point_durability(self):
     p = Pencil(pencilLength=4, eraserDurability=4)
     f = open('test/input/sample_input.txt', 'r')
     input_text = f.read()
     f.close()
     f = open('test/input/sample_output.txt', 'r')
     output_text = f.read()
     f.close()
     written_text = p.pencil_write(text_to_write=input_text)
     self.assertEqual(p.pointDurability, 0)
     self.assertEqual(written_text, output_text)
Beispiel #10
0
    def test_when_pencil_writes_additional_text_it_should_append_to_existing_text(self):
        pencil = Pencil()

        text_part_a = "I'm not the kind of man "
        text_part_b = "who can just walk into a bouncy castle."

        pencil.write(self.paper, text_part_a)
        pencil.write(self.paper, text_part_b)

        expected = text_part_a + text_part_b

        self.assertEqual(expected, self.paper.read())
Beispiel #11
0
    def test_when_pencil_is_sharpened_it_regains_full_durability(self):
        pencil = Pencil(point_durability=4)

        pencil.write(self.paper, "text")
        pencil.sharpen()
        pencil.write(self.paper, "texts")

        self.assertEqual("texttext ", self.paper.read())
Beispiel #12
0
    def test_when_new_word_goes_beyond_end_of_paper_it_should_be_appended_to_the_end(self):
        pencil = Pencil()

        pencil.write(self.paper, "A short phrase")
        pencil.erase(self.paper, "phrase")
        pencil.edit(self.paper, "sentence")

        self.assertEqual("A short sentence", self.paper.read())
    def test_when_pencil_eraser_degrades_fully_it_should_stop_erasing(self):
        eraser = Eraser(durability=4)
        pencil = Pencil(eraser=eraser)

        pencil.write(self.paper, "I am related to Buffalo Bill")
        pencil.erase(self.paper, "Bill")
        pencil.erase(self.paper, "Buffalo")

        self.assertEqual("I am related to Buffalo     ", self.paper.read())
Beispiel #14
0
 def test_sharpen_pencil_by_five_decrease_length_by_four_set_pencil_durability_to_zero(
         self):
     p = Pencil(pencilLength=4, eraserDurability=4)
     p.pencil_sharpen()
     p.pencil_sharpen()
     p.pencil_sharpen()
     p.pencil_sharpen()
     p.pencil_sharpen()
     self.assertEqual(p.pencilLength, 0)
     self.assertEqual(p.pointDurability, 0)
Beispiel #15
0
    def __init__(self):
        super(MainWindow, self).__init__()

        # -- Variables --
        self._h_mirror = False
        self._r_mirror = True
        self._r_mirror_amount = 5

        self._is_fullscreen = False

        self._last_x = 0
        self._last_y = 0

        self._last_buffer_time = time.time()
        self._last_update_time = time.time()

        self._mouse_pressed = False

        # Image that holds the drawing.
        self._buffer_image = None

        # Image that holds the drawing, minus the last stroke (one continuous drag with the mouse).
        self._undo_buffer_image = None

        # Line buffer.
        self._lines = []

        # ---- Initialize the pencils. ----
        self._pencils = []

        for i in range(0, 50):
            self._pencils.append(Pencil(0, 0, 0.2, 0.8, 0.8, 0.5))

        # -- Drawing area --

        self._area = Gtk.DrawingArea()

        # Capture button presses in drawing area.
        self._area.add_events(Gdk.EventMask.BUTTON_PRESS_MASK)
        self.add_events(Gdk.EventMask.KEY_PRESS_MASK)
        self._area.add_events(Gdk.EventMask.BUTTON_RELEASE_MASK)
        self._area.add_events(Gdk.EventMask.POINTER_MOTION_MASK)

        # Connect events.
        self._area.connect('draw', self._draw)
        self.connect('key-press-event', self._key_press)
        self._area.connect('button-press-event', self._button_press)
        self._area.connect('button-release-event', self._button_release)
        self._area.connect('motion-notify-event', self._motion_notify)

        # Visuals update
        GLib.timeout_add(10, self._update)

        self.set_size_request(1000, 500)

        self.add(self._area)
Beispiel #16
0
    def graph_url(self):
        at = "%H:%M_%Y%m%d"
        graph_url = Pencil(begin=self.first_violation.strftime(at),
            until=self.last_violation.strftime(at))
        graph_url.add_metric(self.metricalert.target)
        graph_url.add_metric("threshold(%i)" % self.threshold_violated,
            alias="Threshold", colors="blue,red")

        return graph_url.url(self.metricalert.server.base_url, 586, 306)
Beispiel #17
0
def check_graphite_servers():
    from missioncontrol.models import GraphiteServer
    for server in GraphiteServer.objects.all():
        for metricalert in server.metric_alerts.all():
            metric_url = Pencil(begin=metricalert._from).add_metric(
                metricalert.target).format('json').url(
                server.base_url, 0, 0)
            metric_resp = requests.get(metric_url,
                auth=(server.username, server.password))

            metricalert.check(metric_resp.json)
Beispiel #18
0
    def test_editing_should_replace_erased_word_with_new_text(self):
        pencil = Pencil()

        pencil.write(self.paper, "An       a day keeps the doctor away")
        pencil.edit(self.paper, "onion")

        self.assertEqual("An onion a day keeps the doctor away", self.paper.read())
Beispiel #19
0
    def test_when_new_word_is_too_long_it_should_replace_characters_with_at_symbol(self):
        pencil = Pencil()

        pencil.write(self.paper, "An       a day keeps the doctor away")
        pencil.edit(self.paper, "artichoke")

        self.assertEqual("An artich@k@ay keeps the doctor away", self.paper.read())
Beispiel #20
0
    def test_pencil_cannot_be_sharpened_past_its_length(self):
        pencil = Pencil(point_durability=1, length=2)

        for c in "text":
            pencil.write(self.paper, c)
            pencil.sharpen()

        self.assertEqual("tex ", self.paper.read())
    def test_erasing_should_erase_opposite_direction_of_the_written_order(
            self):
        eraser = Eraser(durability=3)
        pencil = Pencil(eraser=eraser)

        pencil.write(self.paper, "I am related to Buffalo Bill")
        pencil.erase(self.paper, "Bill")

        self.assertEqual("I am related to Buffalo B   ", self.paper.read())
def test_pencil():
    from itertools import product
    comm = MPI.COMM_WORLD
    sizes = (7, 8, 9, 16, 17, 24)
    types = 'fdgFDG'
    atol = dict(f=1e-6, d=1e-14, g=1e-15)
    for typecode in types:
        for M, N, P in product(sizes, sizes, sizes):
            if P % 2: continue
            fft = Pencil(comm, (M, N, P), typecode)
            shape = fft.forward_input_array.shape
            dtype = fft.forward_input_array.dtype
            U = np.random.random(shape).astype(dtype)
            if 0:
                F = fft.forward(U)
                V = fft.backward(F)
            else:
                fft.forward_input_array[...] = U
                fft.forward()
                fft.backward()
                V = fft.backward_output_array

            fft.destroy()
            assert np.allclose(U, V, atol=atol[typecode.lower()], rtol=0)
    def test_when_the_word_to_be_erased_is_not_found_nothing_should_be_erased(
            self):
        eraser = Eraser()
        pencil = Pencil(eraser=eraser)

        text = "Nothing to erase here"
        pencil.write(self.paper, text)
        pencil.erase(self.paper, "Something")

        self.assertEqual(text, self.paper.read())
Beispiel #24
0
def test_editing_replaces_text(initial_text, new_text, output):
    paper = Paper(initial_text)
    pencil = Pencil()
    pencil.edit(paper, new_text)
    assert paper.buffer == output
 def __init__(self):
     self.pencil = Pencil(pencilLength=4, eraserDurability=10)
     self.paper = ''
Beispiel #26
0
 def test_write_one_lowercase_letter_and_one_whitespace_decrease_point_durabitlity_by_one(
         self):
     p = Pencil(pencilLength=4, eraserDurability=4)
     written_text = p.pencil_write(text_to_write="a ")
     self.assertEqual(p.pointDurability, 39999)
     self.assertEqual(written_text, "a ")
Beispiel #27
0
 def test_write_two_lowercase_letter_two_uppercase_letter_decrease_point_durabilty_by_six(
         self):
     p = Pencil(pencilLength=4, eraserDurability=4)
     written_text = p.pencil_write(text_to_write="ABcd")
     self.assertEqual(p.pointDurability, 39994)
     self.assertEqual(written_text, "ABcd")
Beispiel #28
0
 def test_create_a_new_pencil_with_lenght_four_and_eraser_durability_four(
         self):
     p = Pencil(pencilLength=4, eraserDurability=4)
     self.assertEqual(p.pencilLength, 4)
     self.assertEqual(p.eraserDurability, 4)
     self.assertEqual(p.pointDurability, 40000)
Beispiel #29
0
 def test_erase_one_letter_with_white_space_update_the_eraser_durability_have_an_empty_string_with_two_white_spaces(
         self):
     p = Pencil(pencilLength=4, eraserDurability=4)
     erased_text = p.pencil_erase(text_to_erase='a ')
     self.assertEqual(p.eraserDurability, 3)
     self.assertEqual(erased_text, "  ")
Beispiel #30
0
 def test_erase_five_letters_with_eraser_durability_being_less_than_five_update_the_eraser_durability_to_have_a_string_with_one_letter_and_three_white_spaces(
         self):
     p = Pencil(pencilLength=4, eraserDurability=4)
     erased_text = p.pencil_erase(text_to_erase='abcde')
     self.assertEqual(p.eraserDurability, 0)
     self.assertEqual(erased_text, "a    ")
Beispiel #31
0
 def setUp(self):
     self.pencil = Pencil()
Beispiel #32
0
class PencilTest(TestCase):
    def setUp(self):
        self.pencil = Pencil()

    def test_begin(self):
        self.pencil.begin("-2hours")
        self.assertEqual(self.pencil._from, "-2hours")

    def test_y_min(self):
        self.pencil.y_min(32)
        self.assertEqual(self.pencil._yMin, 32)

    def test_y_max(self):
        self.pencil.y_max(42)
        self.assertEqual(self.pencil._yMax, 42)

    def test_line_width(self):
        self.pencil.line_width(2)
        self.assertEqual(self.pencil._lineWidth, 2)

    def test_set_title(self):
        self.pencil.set_title("Pencil")
        self.assertEqual(self.pencil._title, "Pencil")

    def test_set_vtitle(self):
        self.pencil.set_vtitle("Awesomeness")
        self.assertEqual(self.pencil._vtitle, "Awesomeness")

    def test_fgcolor(self):
        self.pencil.set_fgcolor("blue")
        self.assertEqual(self.pencil._fgcolor, "blue")

    def test_hide_legend(self):
        self.pencil.hide_legend(True)
        self.assertEqual(self.pencil._hideLegend, "true")
        self.pencil.hide_legend(False)
        self.assertEqual(self.pencil._hideLegend, "false")

    def test_hide_axes(self):
        self.pencil.hide_axes(True)
        self.assertEqual(self.pencil._hideAxes, "true")
        self.pencil.hide_axes(False)
        self.assertEqual(self.pencil._hideAxes, "false")

    def test_set_template(self):
        self.pencil.set_template("plain")
        self.assertEqual(self.pencil._template, "plain")

    def test_set_font(self):
        self.pencil.set_font("Verdana")
        self.assertEqual(self.pencil._fontName, "Verdana")

    def test_area_mode(self):
        self.pencil.area_mode("stacked")
        self.assertEqual(self.pencil._areaMode, "stacked")

    def test_line_mode(self):
        self.pencil.line_mode("staircase")
        self.assertEqual(self.pencil._lineMode, "staircase")

    def test_set_bgcolor(self):
        self.pencil.set_bgcolor("red")
        self.assertEqual(self.pencil._bgcolor, "red")

    def test_add_metric(self):
        self.pencil.add_metric("pencil.urls.count")
        self.assertEqual(self.pencil._target, ["pencil.urls.count"])

    def test_add_metric_with_color(self):
        self.pencil.add_metric("pencil.urls.count", "000000")
        self.assertEqual(self.pencil._target, ["pencil.urls.count"])
        self.assertEqual(self.pencil._colorList, "000000")

    def test_add_metric_with_alias(self):
        self.pencil.add_metric("pencil.urls.count", alias="urls")
        self.assertEqual(self.pencil._target, ['alias(pencil.urls.count, "urls")'])

    def test_graph_type(self):
        self.pencil.graph_type("pie")
        self.assertEqual(self.pencil._graphType, "pie")

    def test_add_deploy(self):
        self.pencil.add_deploy("pencil")
        self.assertEqual(self.pencil._target, ["drawAsInfinite(pencil)"])

    def test_add_deploy_with_alias(self):
        self.pencil.add_deploy("pencil", alias="deploy")
        self.assertEqual(self.pencil._target, ['alias(drawAsInfinite(pencil), "deploy")'])